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?

有帮助吗?

解决方案

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);
}

其他提示

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
       }
    });
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top