Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have the following variable:

var $scope.abc = [{"a":22,"b":"x"},{"a":33,"b":"y"}];

Is it possible to do a watch on this and watch for just the value of the field a changing?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Is it possible to do a watch on this and watch for just the value of the field a changing

Sure.

If you want to watch only for value a you can define ng-change for (suppose a is a input) input a.

HTML

<div ng-controller="Controller">
   <div ng-repeat="val in abc">
     <input type='text' ng-model='val.a' ng-change="onAChange(val.a)">
     <input type='text' ng-model='val.b'>
  </div>
 </div>

JS

$scope.onAChange = function(mod){
  console.log(mod);
};

Demo 1 Plunker

Other way, to $watch on all object abc and write "deep" watch, something like:

     $scope.$watch(function () {
       return $scope.abc[0].a;
     },
     function (newValue, oldValue) {
     if(newValue == oldValue){return;}

        console.log(newValue, oldValue);

     }, true);

Demo 2 Plunker

As a side note:

You don't need var before $scope.abc.

share|improve this answer
    
Thanks for the comment about var. I will remove that, I was just trying to give an example and made a mistake. Can you explain a bit more about ng-change. I have never used that. Also I am not really sure how to code the .$watch. Thanks – Alan Nov 17 '13 at 8:08
    
@Alan I posted demo in Plunker, you can check, good luck – Maxim Shoustin Nov 17 '13 at 8:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.