Question

I have mentioned my HTML.

<div ng-controller="myCtrl">
     <input type=text ng-model="name">
</div>

in my JS file

function myCtrl($scope){
     $scope.name=//this field comes from DB say 'ABC'
}

My question :

When my html is get loaded, it will disply "ABC" in textbox. Which is fine. Now if user change that name to "XYZ", So $scope.name value is "XYZ". So I need to identify that input value is changed. Previous value is "ABC" and now the value is "XYZ" how we can figured it out that value is changed?

Était-ce utile?

La solution

You can use the ngChange directive.

<input type="text" ng-model="name" ng-change="change()">

Then, in your controller, add a method called change to the scope:

$scope.change = function () {
    console.debug('Changed to ' + $scope.name);
}

Autres conseils

Do it like this:

function myCtrl($scope){
    $scope.$watch('name', function(newValue, oldValue){

    });
}
function myCtrl($scope){
    var dbName= 'ABC'; //this field comes from DB say 'ABC'
    $scope.name= dbName;
    $scope.$watch('name', function(newValue) {
       if(newValue != dbName) {
           // do something
       }
    });
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top