Fire ng-change event for textbox other than keypress or keydown
Solution 1
You can use data-ng-model-options directly to achieve your requirement. Like,
here you can get full reference of data-ng-nmodel-options https://docs.angularjs.org/api/ng/directive/ngModelOptions
this directive will work for the versions angular 1.4.x ( from 1.4.x version it suppports)
Solution 2
You can use ng-blur
and you should change your $watch
:
HTML:
<div ng-app="myApp" ng-controller="dummy">
<input type="text" ng-model="model.value" ng-blur="update()"/>
</div>
JS:
angular.module('myApp', [])
.controller('dummy', ['$scope', function ($scope) {
$scope.model = {
value: 'hello'
};
$scope.update = function () {
console.log("changed!");
};
$scope.$watch('model.value', function (nval, oval) {
if (oval !== nval) {
$scope.update();
}
});
}]);
satish kumar V
Updated on August 21, 2022Comments
-
satish kumar V almost 2 years
I want to update a text value when the value is changed by using some method, the method should not call when I am still typing text but when exit from the textbox or change it through script code:
<input type="text" ng-model ="model.value" ng-change = "update()"/>
what should contain update() method,
what I tried is : wrote a directive for this textbox, and
scope.$watch('model', function(nval,oval){ if ((oVal === undefined) && (nVal != undefined)){ update() } });
It is calling for the first time, but not whenever the model value changed.
I want to call update method whenever the model.value changed, but not while entering text