AngularJS.调用 angular-ui 模态时清除 $timeout [英] AngularJS. Clear $timeout when invoking angular-ui modal
本文介绍了AngularJS.调用 angular-ui 模态时清除 $timeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在模态控制器中有几个 $timeout
表达式
App.controller('ModalCtrl', function ($scope, $timeout) {for (var i = 0; i <10; i++) {(功能 () {var timer = $timeout(function () {console.log('定时器')}, 1000);})()}})
我需要在调用模态时清除所有计时器:
App.controller('MainCtrl', function ($scope, $modal, $timeout) {$scope.showMap = 函数 () {var modal = $modal.open({templateUrl: 'modalap.html',控制器:'modalCtrl',})modal.result.then(function () {//modal 解析时触发}, function () {//当模态调用时触发});} })
我该怎么做?
PS 抱歉代码格式错误.我不知道为什么,但我无法更好地格式化它.我在这里复制了代码:
解决方案
$timeout
服务返回一个 Promise
对象,可用于取消超时.
>
//开始超时var promise = $timeout(function() {}, 1000);//停止挂起的超时$timeout.cancel(promise);
要取消所有挂起的超时,您需要维护一个承诺列表,并在打开模态时取消完整列表.
I have several $timeout
expressions in Modal controller
App.controller('ModalCtrl', function ($scope, $timeout) {
for (var i = 0; i < 10; i++) {
(function () {
var timer = $timeout(function () {
console.log('timer')
}, 1000);
})()
}
})
I need to clear all the timers when invoking the modal:
App.controller('MainCtrl', function ($scope, $modal, $timeout) {
$scope.showMap = function () {
var modal = $modal.open({
templateUrl: 'modalap.html',
controller: 'modalCtrl',
})
modal.result.then(function () { //fires when modal is resolving
}, function () { //fires when modal is invoking
});
} })
How can I do that?
PS Sorry for bad code formatting. I don't know why but I cant format it better. I duplicated code here:
解决方案
The $timeout
service returns a Promise
object which can be used to cancel the timeout.
// Start a timeout
var promise = $timeout(function() {}, 1000);
// Stop the pending timeout
$timeout.cancel(promise);
To cancel all pending timeouts, you need to maintain a list of promises and cancel the complete list when you open the modal.
这篇关于AngularJS.调用 angular-ui 模态时清除 $timeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文