Shardul Pendse
Shardul Pendse

Reputation: 304

how to identify whether the textbox value is change or not?

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?

Upvotes: 0

Views: 1992

Answers (3)

Alexander Nenkov
Alexander Nenkov

Reputation: 2910

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

Upvotes: 0

Joel Skrepnek
Joel Skrepnek

Reputation: 1661

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

Upvotes: 1

karaxuna
karaxuna

Reputation: 26940

Do it like this:

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

    });
}

Upvotes: 0

Related Questions