How do I stop $watching in AngularJS?

后端 未结 2 686
悲&欢浪女
悲&欢浪女 2020-12-13 23:30

I can set up a $watch on an AngularJS scope to be notified when the expression I am interested in has changed. But how do I stop watching once I lose interest?

2条回答
  •  盖世英雄少女心
    2020-12-13 23:54

    As an additional comment, but not answer - this is the same principle for killing event listeners as well.

    var unregister = $rootScope.$on("listen-for-something-cool", function($event, params) {
       //Do cool stuff with params, then kill it
       unregister();
    };
    

    Just an addition I thought would be cool for anyone else to know...

提交回复
热议问题