[英]angularjs callback / wait for function to finish
I'm i bit stuck with function which is assigned to the $scope variable
. 我有点被分配给$scope variable
函数所困扰。 need to wait until it's finished. 需要等到完成为止。
This is the function assigned to $scope variable
: 这是分配给$scope variable
的函数 :
$scope.ChooseHome = function() {
ModalWindowService.openChooseHomeDialog($scope);
}
in ModalWindowService I have: 在ModalWindowService中,我有:
function openChooseHomeDialog($scope) {
$scope.animationsEnabled = true;
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: '/view/user/ChooseHomeDialog.html',
controller: 'chooseHomeDialogController',
windowClass: 'detail-modal-window'
});
modalInstance.result.then(function (CondoID) {
$scope.choosenCondoID = CondoID;
});
}
what I want to do is properly receive this variable '$scope.choosenCondoID' after I get it from modal in '$scope.ChooseHome' function and do some work with it. 我想做的是在我从'$ scope.ChooseHome'函数中的模态中获取变量$$$ scope.choosenCondoID并对其进行一些处理之后。
smth like this options: 像这样的选项:
$scope.ChooseHome = function() {
ModalWindowService.openChooseHomeDialog($scope).then(*do my stuff*); /
ModalWindowService.openChooseHomeDialog($scope).success(*do my stuff*);
}
But it's not working, I'm getting errors like this: 但这不起作用,我收到这样的错误:
'TypeError: (intermediate value).success is not a function'
Try this: 尝试这个:
function openChooseHomeDialog($scope) {
$scope.animationsEnabled = true;
return $modal.open({
animation: $scope.animationsEnabled,
templateUrl: '/view/user/ChooseHomeDialog.html',
controller: 'chooseHomeDialogController',
windowClass: 'detail-modal-window'
}).result;
}
$scope.ChooseHome = function() {
ModalWindowService.openChooseHomeDialog($scope).then(*do my stuff*);
}
And...like I commented...passing on the $scope to your service is a bad idea. 而且...就像我评论的那样...将$ scope传递给您的服务是一个坏主意。 You should only use $scope in your controller to glue your data to the view. 您只应在控制器中使用$ scope将数据粘贴至视图。 A service just has to do something or return something which you can use. 服务只需要做某事或返回您可以使用的东西。
To complicate it a bit further..you might want to look into the controllerAs syntax as well to be future proof with your code ;) 进一步使它复杂化..您可能还希望研究controllerAs语法,以便将来对代码进行验证;)
you must return your promise from openChooseHomeDialog function 您必须从openChooseHomeDialog函数返回您的诺言
...
return modalInstance.result.then(function (CondoID) {
$scope.choosenCondoID = CondoID;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.