javascript - AngularJS $watch not working into object -
i not able watch work when placed on element within array;
see example: http://jsfiddle.net/zwrza551/2/
<div ng-app> <div ng-controller="test"> name: <input type="text" ng-model="customer.name" /> <hr/> <div ng-repeat="address in customer.addresses"> <input type="text" ng-model="address.name" /> <input type="text" ng-model="address.zipcode" /> <hr /> </div> </div> </div>
function test($scope){ $scope.customer = { name: 'customer 1', addresses: [ { name: 'address 1', zipcode: 123456, city: null, state: null }, { name: 'address 2', zipcode: 234567, city: null, state: null }, { name: 'address 3', zipcode: 456787, city: null, state: null }, { name: 'address 4', zipcode: 675684, city: null, state: null } ] }; $scope.$watch('customer.name', function(newname, oldname){ console.log(newname); }); $scope.$watch('address.zipcode', function(newcode, oldcode){ console.log(newcode); //make request webservice, addres zipcode set city , state address. }); }
on name of client can, since array of addresses not work.
try go that:
$scope.$watch('customer.addresses', function(newcode, oldcode){ (... every address in customer.addresses) { // check if city , state null , request if true } }, true);
don't forget turn deep watch switch on. (the 3rd argument true
) switch enables checking witch angular.equals
instead of comparing references, results in array beingness watched on changes in elements.
javascript angularjs undefined watch
No comments:
Post a Comment