4

All:

I am new to AngularJS, current what I want to do is to figure out which line cause the $watch handling function get called.

For a small example:

I set $scope.name = "" and call a function changeName():

function changeName(){
    $scope.name = "new Name";
    ...

    $http.get("someurl")
         .then(function(data){ $scope.name = data.newname;   })
}

$scope.$watch("name", function(newName, oldName){
    handlingfunctionHere();
});

There is multiple changes to $scope.name which make $scope.$watch trigger, but when I debug in the chrome, I can not tell which action lead to that triggering from call stack, could anyone show me how to debug this?

Thanks

10
  • @PankajParkar Thanks, honestly I am too new to AngularJS to fully understand this, could you give an Example or fiddle? Commented Sep 10, 2015 at 17:09
  • but that doesn't give fully assurity from where value gets updating.. Commented Sep 10, 2015 at 17:15
  • you can use a different variable if you want to track when $scope.name changes after $http Commented Sep 10, 2015 at 17:19
  • @maddog Thanks, but what I want is like Pankaj Parkar said, I want to know where does that name variable get updated which leads to the triggering of $watch Commented Sep 10, 2015 at 17:23
  • @Kuan basically I was suggested you to use $parser & $formatters that does give an access to $viewValue & $modalValue stackoverflow.com/questions/22841225/… . But its not possible with this way.. Commented Sep 10, 2015 at 17:31

1 Answer 1

3

Sorry that I will need to tell you this, maybe you will get disappointed because as you said you are new with AngularJS, but the real truth is that you cannot know with simple debugging as putting a breakpoint in the watch handler.

As a reference I will add this post from Misko itself where he explains how angular data binding works and what are the benefits and the drawbacks from it. This is one of the drawbacks that is not mentioned but still for me not a big deal because IMO the benefits are bigger.

This just makes the debugging harder because you will need to manually find all the places where change can be done and put a breakpoint there. It is also possible several changes to be done before the watch handler is called.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is a very disappointed answer, but I will accept this truth until someone proposes a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.