加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在角度,如何使用取消$间隔用户事件,如页面更改?

发布时间:2020-12-17 08:41:17 所属栏目:安全 来源:网络整理
导读:Angular documentation约$ interval正在说: Note: Intervals created by this service must be explicitly destroyed when you are finished with them. 但它不解释如何销毁$ interval。 如果例如我有一个包含这个代码的指令: $interval(function() { for
Angular documentation约$ interval正在说:

Note: Intervals created by this service must be explicitly destroyed when you are finished with them.

但它不解释如何销毁$ interval。

如果例如我有一个包含这个代码的指令:

$interval(function() {
    for (var i in myArray) {
        // do domething
    }
},5000);

当用户更改页面时,我如何销毁它?

每当用户更改页面时,与路由控制器(在下面的示例中为/ page1)相关联的范围将被发送 a $destroy event.您可以在侦听器中取消该事件的$ interval:
app.config(function ($routeProvider) {
     $routeProvider.when('/page1',{
          template: '<div>Page Content</div>',controller: PageController
      });
     // ...
});

function PageController($scope,$interval) {
    var intervalPromise = $interval(function () { /* ... */ },5000);      
    $scope.$on('$destroy',function () { $interval.cancel(intervalPromise); });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读