angular-bind-notifier icon indicating copy to clipboard operation
angular-bind-notifier copied to clipboard

watching undefined variable causes many "fake" watchers to be created

Open yonihei opened this issue 9 years ago • 2 comments

In short: using the plugin on an undefined object causes the number of watchers to grow for no reason constantly.

ng-if=":auto:d.fakeVariable"

http://jsbin.com/titozudubo/edit?html,js,output (i hope it is working, but ill write here anyway the problem)

Explanation: i use a short code for finding the number watchers. i hope its is not buggy. Each time that the key "auto" has changed, it creates more watchers for no reason because d.fakeVariable is not defined.

here is the code for counting the number of watchers:

var getWatchers = function(root) { root = angular.element(root || document.documentElement); var watcherCount = 0;

  var getElemWatchers = function(element) {
    var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
    var scopeWatchers = getWatchersFromScope(element.data().$scope);
    var watchers = scopeWatchers.concat(isolateWatchers);
    angular.forEach(element.children(), function (childElement) {
      watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
    });
    return watchers;
  }
  
  var getWatchersFromScope = function(scope) {
    if (scope) {
      return scope.$$watchers || [];
    } else {
      return [];
    }
  }
 
  return getElemWatchers(root);
};

console.log(getWatchers ().length);

yonihei avatar Jan 12 '17 13:01 yonihei

You can override this until fixed by doing ng-if=":auto:(!!d.fakeVariable)"

fpoljak avatar Jun 06 '17 14:06 fpoljak

Any updates on this? It seems to occur because the bindNotifier logic is piggybacking the one time binding workflow, and the default behaviour in one-time bindings is to keep a watcher until the value is undefined https://docs.angularjs.org/guide/expression#value-stabilization-algorithm

Heres a smaller example adapted from @yonihei 's jsbin https://jsbin.com/hovurekaqo/1/edit?html,js,output you can see the watcher count go up every 1s

ghost avatar Oct 12 '18 01:10 ghost