AngularJS.调用angular-ui模态时清除$ timeout

use*_*402 56 timeout angularjs angular-ui

$timeout在Modal控制器中有几个表达式

App.controller('ModalCtrl', function ($scope, $timeout) {
    for (var i = 0; i < 10; i++) {
        (function () {
            var timer = $timeout(function () {
                console.log('timer')
            }, 1000);
        })()
    }
})
Run Code Online (Sandbox Code Playgroud)

我需要在调用模态时清除所有计时器:

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
        });
    } })
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

PS对不起代码格式错误.我不知道为什么,但我不能更好地格式化它.我在这里重复了代码:

nul*_*ull 132

$timeout服务返回一个Promise可用于取消超时的对象.

// Start a timeout
var promise = $timeout(function() {}, 1000);

// Stop the pending timeout
$timeout.cancel(promise);
Run Code Online (Sandbox Code Playgroud)

要取消所有挂起的超时,您需要维护一个承诺列表,并在打开模式时取消完整列表.