Bhojendra Nepal Bhojendra Nepal - 3 months ago 5
AngularJS Question

how does this break the watching value?


Changing the above code breaks the watching value on clicked (I'm unclear because breakit still pushing the value to nums):

$scope.breakit = $scope.$watch('num',function(){

But I'm unclear how does this break the watching value?

Full code:

<div ng-controller="MainController">
<div>Num: {{num}}</div>
<div>Nums: {{nums}}</div>
<button ng-click="increment()">Increment</button>
<button ng-click="breakit()">Break It</button>

var app = angular.module('app',[]);
$scope.num = 0
$scope.nums = []
$scope.increment = function(){
$scope.breakit = $scope.$watch('num',function(){

So, using
why does this stops pushing the
clicking to break it button and continue clicking to increment button?

You may check this video.


The documentation for $ says:


function() Returns a deregistration function for this listener

When you call $, it returns a function that you can call at any time to unregister it.

Your controller assigns that function to $scope.breakit, so when $scope.breakit is called, the watch is turned off.


There was a question in the comments about how $scope.break pushes values to the nums array. The answer is that it doesn't. $ takes care of that all on its own, and $scope.break is used as a way to turn that process off.