by changing a service from 'outside' angular js the watchers stop working

老子叫甜甜 提交于 2019-12-02 06:35:42

As already mentioned, you need to use $apply:

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.

For example:

var element = angular.element(document.querySelector('body'));
var service = element.injector().get('myService');
var scope = element.scope();

scope.$apply(function () {
  service.somevar = true;
});

Demo: http://plnkr.co/edit/Iy3CiRzDdxFTwVq68JWi?p=preview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!