Shirgill Farhan Ansari Shirgill Farhan Ansari - 1 year ago 74
AngularJS Question

How to watch all properties of an object which is generated dynamically?

Consider the snippet:

<div ng-app="myApp" ng-controller="myCtrl">

<tr ng-repeat = "value in array" >
<td><input ng-model="formdata[value]" /></td>


var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

$scope.array = [1, 2, 3, 4, 5];
function() {
// some code goes here
}, true

The ng-model for each row is generated dynamically as seen clearly.


Now I want to put a watch on all the properties of this object(formdata). I did like above but the watch never gets fired.

Please suggest.

Answer Source

You didn't create formdata in the scope, so ng-repeat creates it, in its own child scope. And the formdata is thus not in your scope, and thus can't be watched.

I would start at 0 rather than 1, and initialize the array in the scope:

$scope.formdata = [];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download