AngularJS ngChange trigger onblur
42,725
Solution 1
Starting with release 1.2.0rc1 there is an ng-blur
directive
jsfiddle: http://jsfiddle.net/9mvt8/6/
HTML:
<div ng-controller="MyCtrl">
<input type='text' ng-blur='blurCount = blurCount + 1'/>
<input type='text' ng-blur='blurCount = blurCount + 1' />
blur count: {{blurCount}}
</div>
Script:
function MyCtrl($scope) {
$scope.blurCount = 0;
$scope.name = 'Superhero';
}
Solution 2
Use ng-model-options
$scope.onchange = function () {}
<input type="text" ng-model="x" ng-change="onchange()" ng-model-options="{updateOn: 'blur'}"/>
Author by
9blue
Updated on January 19, 2020Comments
-
9blue over 4 years
for
ng-Change
, is there way to trigger it only when onblur
? Similar to jqueryon('change')
?I am seeking for a pure
angular
way of doing this. -
9blue over 10 yearsthis is not exactly what I want, if I didnt change the value but lose focus of that field, it will still trigger the function
-
DusanV over 10 yearsYou would handle that yourself in the blur handler. If the value changes do what you need to, otherwise do nothing.
-
Niels Steenbeek about 10 yearsThis official one will prevent the need to handle the change logic inside the blur handler: <input type="text" ng-model="x" ng-change="onchange()" ng-model-options={updateOn: 'blur'}"/>
-
John Rix almost 10 yearsThat is a much more elegant solution. It only works in v1.3 though (which is still in beta).
-
dman over 9 yearsng-dirty is used for the whole form rather than just a input box.
-
Mubashar Shahzad about 5 yearsReally saved time and this should be the correct answer.