Javascript 角模态返回承诺
在用户执行操作之前,我想向用户显示一个弹出警告模式(在本例中删除)。如果他们在模式中单击Javascript 角模态返回承诺,javascript,angularjs,promise,Javascript,Angularjs,Promise,在用户执行操作之前,我想向用户显示一个弹出警告模式(在本例中删除)。如果他们在模式中单击确定,它将继续,如果他们单击取消,它将执行其他操作 为此,我尝试将模式承诺沿着链传递回初始调用$rootScope.openDirections('warning')。然后(…),以便我可以决定在那里做什么 但是我得到了一个错误: $rootScope.openDirections(…)。则不是作用域中的函数。$Scope.deleteObj 告诉我承诺不会被退回: $scope.deleteObj
确定
,它将继续,如果他们单击取消
,它将执行其他操作
为此,我尝试将模式承诺沿着链传递回初始调用$rootScope.openDirections('warning')。然后(…
),以便我可以决定在那里做什么
但是我得到了一个错误:
$rootScope.openDirections(…)。则不是作用域中的函数。$Scope.deleteObj
告诉我承诺不会被退回:
$scope.deleteObj = function (id, type) {
$rootScope.openDirections('warning').then(function (res) {
// Do Stuff
}, function (err) {
console.log('ERROR', err);
});
这将调用$rootScope函数:(我看到这里的对象类型是一个承诺……那么它现在不应该返回到原始调用方吗?)
调用模式
工厂打开方向模式:
return {
...
showDirections: function(type) {
return $modal.open({
backdrop: 'static',
templateUrl: 'modal.html',
controller: 'directionsCtrl',
resolve: {
// Display custom modal template
obj: function () {
if (type === 'markdown') {
return {
template: '/modals/directions.html'
};
} else {
return {
template: '/modals/message.html'
};
}
},
testID : function () {
return 'WHAT DO I RETURN HERE?';
}
}
});
}
它呈现message.html
模式模板:
<div class="admin-directions">
<div class="directions">
ARE YOU SURE YOU WANT TO DELETE THIS?
</div>
<p class="dismiss-admin-help" ng-click="okay()">Okay</p>
<p class="dismiss-admin-help" ng-click="cancel()">Cancel</p>
</div>
$modal.open返回一个$modalInstance,该$modalInstance具有一个名为result的属性,该属性是promise。您必须对该属性调用.then()。因此,在您的情况下,您需要:
$rootScope.openDirections('warning').result.then(function (res) {
// Do Stuff
}, function (err) {
console.log('ERROR', err);
});
或者,在openDirections方法中,您可以返回承诺:
showDirections: function(type) {
return $modal.open({
//all your current stuff stays the same here
}).result; //<-- notice that you are now returning the promise
}
显示方向:函数(类型){
返回$modal.open({
//你现在所有的东西在这里都是一样的
}).result;//好的,我的$modal
实例不在同一个控制器中,而是在它自己的工厂中。因此,我要在工厂中准确地解析什么才能传递回原始调用方?这是什么意思?当您按照我的建议执行时,您的错误是否消失了?$scope.ok
和$scope.cancel
按钮不再适用于控制器中的modalInstance。您应该使用$modalInstance.close(),它将触发解析承诺。$modalInsance.dismise()将导致它被拒绝。无论您传递到这些方法中的是什么,都将是在调用.then()时传递给附加处理程序的参数。
$rootScope.openDirections('warning').result.then(function (res) {
// Do Stuff
}, function (err) {
console.log('ERROR', err);
});
showDirections: function(type) {
return $modal.open({
//all your current stuff stays the same here
}).result; //<-- notice that you are now returning the promise
}