Fire ng-change event for a text field other than a keystroke or keystroke

I want to update the text value when the value is changed using some method, the method should not call when I still print the text, but when I exit the text field or change it through the script code:

<input type="text" ng-model ="model.value" ng-change = "update()"/>

what the update () method should contain,

what I tried: wrote a directive for this text box and

scope.$watch('model', function(nval,oval){
   if ((oVal === undefined) && (nVal != undefined)){
     update()
   }
});

It is called the first time, but not every time the model value changes.

I want to call the update method whenever the value of model.value changes, but not when entering text

+4
source share
2 answers

You can use the data-ng-model options directly to achieve your requirements. How,

data-ng-nmodel https://docs.angularjs.org/api/ng/directive/ngModelOptions

angular 1.4.x( 1.4.x )

+2

ng-blur, $watch:

JSFiddle

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();
        }
    });
}]);
+2

Source: https://habr.com/ru/post/1599831/


All Articles