Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - ng-change not firing when ng-model is changed

Tags:

The input is the following:

<input type="text" ng-model="repair.test"  ng-change="action()" /> 

The action() is executed when I manually type and change the input. However if I change the repair.test value by some other function programmatically, it doesnt fire the ng-change's action. I have read the angular tutorial and it's probably the expected behavior.

https://docs.angularjs.org/api/ng/directive/ngChange

"The expression is not evaluated when the value change is coming from the model." <- I need this too. How can I fire an action in controller, when the model changes in any way? (typing in input or by any other function)

Thanks for the help.

Edit:

The model value is actually the form.$valid, which has it's own Form controller around it (I think), that is why I used the action function to try to pass the value to the parent controller. So $scope.$watch at the moment doesn't work, only when it is initialised.

like image 666
tomazahlin Avatar asked Jul 17 '14 11:07

tomazahlin


People also ask

How do you use NG on change?

The ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.

What is NG-model and Ng-change?

Ng-change is a directive in AngularJS which is meant for performing operations when a component value or event is changed. In other words, ng-change directive tells AngularJS what to do when the value of an HTML element changes. An ng-model directive is required by the ng-change directive.

What is difference between ng-model and Ng bind?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.

How does ng-repeat work?

The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.


1 Answers

ngChange is just for the input, if you want to listen the model do like this

$scope.$watch('repair.test', function(newvalue,oldvalue) {              }); 
like image 145
ThomasP1988 Avatar answered Oct 03 '22 07:10

ThomasP1988