The ngChange
directive from angular.js
is used to execute an Angular expression when there is a change in the input due to user interaction.
This directive adds the evaluated expression to the list of view change listeners.
It works by adding the expression to the list of listeners that need to be executed when the $modelValue
is updated one at a time:
var ngChangeDirective = valueFn({
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
ctrl.$viewChangeListeners.push(function() {
scope.$eval(attr.ngChange);
});
}
});
Unlike other directives like ngClick
, ngChange
does not pass any events around. It simply executes the expression after the model value has been set.
In contrast, ngClick
passes the $event
into the click handling function because it deals with DOM events:
forEach(
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
function(eventName) {
// Code for handling different event types and passing $event
}
);
When an event occurs, the DOM event object is passed as $event
to the handling function, where it can be accessed and used as needed.