首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何暂停和恢复作用域。$$watchers?

如何暂停和恢复作用域。$$watchers?
EN

Stack Overflow用户
提问于 2015-05-18 22:48:18
回答 2查看 990关注 0票数 1

我在angularjs1.4中创建了复杂的表单。而且我需要使用模式对话框,但是主窗体的观察器对性能影响很大。我使用next方法禁用观察器:

代码语言:javascript
复制
.directive('suspendable', ['$timeout', function ($timeout) {
return {
    link: function (scope) {
        // Heads up: this might break is suspend/resume called out of order
        // or if watchers are added while suspended
        var watchers;
        var depth = 0;
        scope.$on('suspend', function (event, args) {
            if(watchers){
                return;
            }
            depth = args.depth;
            watchers = scope.$$watchers;
            scope.$$watchers = [];              
            console.log(depth + ' suspend ' + (watchers?watchers.length:0));
        });

        scope.$on('resume', function (event, args) {
            if (watchers && (depth == args.depth)) {
                scope.$$watchers = watchers;
                scope.$$watchersCount = watchers.length;
                watchers = void 0;                  
                console.log(depth + ' resume ' + (watchers.length));
            }
        });
    }
};}])

我将suspendable指令添加到main form中的所有指令中,并在显示模式对话框之前调用broadcast event suspend,在对话框关闭后调用broadcast event resume。但是我在恢复后没有观察者的功能。

EN

回答 2

Stack Overflow用户

发布于 2015-05-19 16:58:09

我更改了resume

代码语言:javascript
复制
scope.$on('resume', function (event, args) {
            if (watchers && (depth == args.depth)) {
                if (!scope.$$watchers ) {
                    scope.$$watchers = watchers;
                } else {
                    scope.$$watchers = scope.$$watchers.concat(watchers);
                }
                console.log(depth + ' resume ' + (watchers.length));
                watchers = void 0;
            }
        });

这对我很有帮助!

票数 0
EN

Stack Overflow用户

发布于 2019-02-04 20:16:39

https://github.com/angular/angular.js/commit/41d5c90f170cc054b0f8f88220c22ef1ef6cc0a6:AngularJS 1.7有一个新特性--暂停和恢复作用域观察者的能力

scope.$suspend()的调用将阻止在摘要期间执行此范围子树上的监视器。

调用scope.$resume()将恢复监视程序的执行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30306336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档