Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 816
alun Avatar asked Jun 10 '13 11:06

alun


1 Answers

Check this workaround

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

$scope.catName = 'Le cat'

$scope.$watch('catName', function (newValue, oldValue) {
    if(oldValue === newValue){
        return;
    }
    //[...]
})
like image 167
Abhishek Nandi Avatar answered Nov 10 '22 00:11

Abhishek Nandi