How do I stop $watching in AngularJS?

后端 未结 2 684
悲&欢浪女
悲&欢浪女 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-14 00:04

    When calling $watch a function is returned that unregisters the bound expression.

    E.g., to watch a variable foo only change once:

    var unregister = $scope.$watch('foo', function () {
      // Do something interesting here ...
      unregister();
    });
    

    Hope that helps :-)

提交回复
热议问题