我在angularjs1.4中创建了复杂的表单。而且我需要使用模式对话框,但是主窗体的观察器对性能影响很大。我使用next方法禁用观察器:
.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。但是我在恢复后没有观察者的功能。
发布于 2015-05-19 16:58:09
我更改了resume
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;
}
});这对我很有帮助!
发布于 2019-02-04 20:16:39
https://github.com/angular/angular.js/commit/41d5c90f170cc054b0f8f88220c22ef1ef6cc0a6:AngularJS 1.7有一个新特性--暂停和恢复作用域观察者的能力
对scope.$suspend()的调用将阻止在摘要期间执行此范围子树上的监视器。
调用scope.$resume()将恢复监视程序的执行。
https://stackoverflow.com/questions/30306336
复制相似问题