문제

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