javascript - Odd behavior in $scope.$watch -


i have following $scope.watch function:

$scope.logchecked = [];  $scope.selectall = false; $scope.$watch('selectall', function(selectall) {        console.log($scope.logchecked.length, $scope.logchecked, selectall); }); 

the console.log() output correctly enumerates $scope.logchecked while $scope.logchecked.length shows 0

0 [pf2n1448: false, cc7a1340: false, cc7a1328: false, pf2n1424: false, if2n1328: false…] true

i modifying logschecked following checkbox:

<td><input type="checkbox" ng-model="logchecked[log.batchid]"/></td> 

what's going on here?

because logchecked array nothing inside (hence length 0), attached properties (arrays still objects).

var foo = [];    foo.bar = 'baz';    document.write(foo.length); // 0    document.write(foo.bar); // "baz"

the part isn't obvious log.batchid isn't numeric. logchecked[log.batchid] attaches value property of logchecked, not inside array.

your problem solved making logchecked object ({}) instead. number of things in object, use object.keys($scope.logchecked).length


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -