[英]Angular js - wait in method for variable change
我的控制器中有作用域属性
isLoading : boolean = true;
当isLoading为false时应执行的methodA,如果isLoading为true且调用methodA,则应等待isLoading更改为false,然后继续执行
methodA() {
// if isLoading == true wait for it to change to false
// then do some stuff
}
我该如何实现?
解决方案1:使用观察者
$scope.$watch('isLoading', function(n){
if(n){
methodA();
}
}
)
解决方案2:如果isLoading
绑定到某个html视图中的ng-model
(例如,输入的ng-model
)。 在html中的ng-change
(该输入)上调用someotherMethod
。 和someotherMethod
将是这样的:
function someotherMethod(){
if(isLoading){
methodA()
}
}
您还可以使用一个Promise对象,并在响应中触发任何您喜欢的东西。
$ http默认情况下会返回一个Promise对象,因此您可以执行以下操作:
在您的工厂/服务中:
foo: function($http){
return $http({
method: 'POST',
url: API_PATH,
data: { email: email, password: password }
});
}
现在,在您的控制器中以这种方式使用它:
foo().then(
function success(response){
// fire your method (methodA)
},
function error(error){
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.