I want to watch an object x to see if any of its properties change. The properties are checkboxes. If a user checks the box, it changes the checked property (prop1) to 1.
Each property has certain content within, so if enabled, it should expand the height of the containing element to account for that new content.
$scope.x = {
prop1: 0,
prop2: 0,
prop3: 0,
prop4: 0,
};
I watch to see if any changes are made in x. If so, the height of the element is updated:
$scope.$watchCollection('x', function(embed){
$scope.x.height = $scope.getHeight();
});
Get height is called, which calls checkOptions() to add to specified buffer space to the element:
$scope.getHeight = function () {
$scope.checkOptions();
return Math.ceil(($scope.totalElements / $scope.totalCols) * elementHeight);
};
$scope.checkOptions = function () {
if ($scope.x.prop1 === 1) {
elementHeight += 50px;
}
...other properties
The HTML uses a checkbox, checking for true/false:
<fieldset class="modal-checkbox">
<input type="checkbox"
id="element-prop1"
ng-model="x.prop1"
ng-true-value="1"
ng-false-value="0">
<label for="element-prop1">
<span>Element Prop 1</span>
</label>
</fieldset>
When I click the checkbox, it keeps adding to the height, and finally:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: [[{"msg":"fn: regularInterceptedExpression","newVal":39,"oldVal":38},{"msg":"fn: expressionInputWatch","newVal":"6168","oldVal":"6006"}],'
I checked Angular $watchCollection, and it seems to do exactly what I want... watch the collection within x object to look for changes...
and $digest says:
Processes all of the watchers of the current scope and its children. Because a watcher's listener can change the model, the $digest() keeps calling the watchers until no more listeners are firing. This means that it is possible to get into an infinite loop.
I don't see why I am getting the loop.
$scope.$watch('x', function(embed){}, true);instead? In my experience$watchCollectionhas never quite worked. The$watchwith object equality set totruewill do a deep watch of all the prop values. - Davin Tryon... other propertiescode. If you are mutating anything that is being watched you'll kick off another cycle and enter a loop. - Davin Tryonheighttoxobject, which is mutated, which kicked off another watch. - Growler$watch, use anng-changeon the checkbox... - Davin Tryon