alun
alun

Reputation: 3441

Skip $watch initial call

So apologies in advance for the terrible title. I don't really know all the correct angular terminology for these things.

I have code like this in a scope:

$scope.catName = 'Le cat'

//<-- Magic goes here

$scope.$watch('catName', function () {
    //[...]
})

Now since angular waits until the next digest (is that the correct term?) to evaluate the watch, my initial assignment ('Le cat') will trigger the watch.

I would like this assigment to not trigger the watch, but changes after this to do so.

Is there some way to reset the 'dirty state' of catName?

Js-fiddle: http://jsfiddle.net/7DNrD/1/

Upvotes: 7

Views: 2654

Answers (1)

Abhishek Nandi
Abhishek Nandi

Reputation: 4275

Check this workaround

http://jsfiddle.net/7DNrD/5/

$scope.catName = 'Le cat'

$scope.$watch('catName', function (newValue, oldValue) {
    if(oldValue === newValue){
        return;
    }
    //[...]
})

Upvotes: 16

Related Questions