4

I am new to AngularJS and trying to set up a function that gets called upon whenever a variable is changed.

Currently I have a dropdown-menu, with ng-model bound to a $scope.userRating. And I am trying to get a function that gets called immediately when the user changes the value using the dropdown.

I have been looking at $watch, but not quite sure on how to get it to work. I also tried to make a ng-click function on the in the html, but ng-clicks don't seem to trigger.

2 Answers 2

10

You should use $watch . Example-

 $scope.$watch('modalVarible',function(newVal,oldVal){
//do your code
}

Here on the change of variable 'modalVarible' this watch will be called.

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

3 Comments

I tried doing that Anita. Just a quick $scope.$watch((userRating), function(newvalue, oldvalue){ console.log(userRating); }) However, it seems to only be called when I visit the partial, and not when changing the variable :/
Then what error it gives ? And it should be $scope.$watch('userRating',.. without brackets
That worked! Thanks, apparently it was just the formatting of what I had in the paranthesis. Thank you Anita!
0

we can use $watchGroup in controller of our directive to bind $scope variable changes (bidirectional)

$scope.$watchGroup(['var1','var2'],function () {

});

you can also check this question.

Comments

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.