Question

I have a Parent checkbox that gets checked when a child checkbox is clicked. I want this parent checkbox to in-turn bind with a third checkbox. But the 2 way binding is not working when the change is due to ng-checked. Here is the template:

<div><input type="checkbox" ng-checked="childCheckbox" ng-model="parentCheckbox" /> Parent Checkbox</div>
<div><input type="checkbox" ng-model="childCheckbox" /> Child Checkbox</div>
<div><input type="checkbox" ng-model="parentCheckbox" /> A third Checkbox binding with Parent Checkbox</div>

Here is the working sample in jsfiddle: http://jsfiddle.net/Alien_time/vy5UM/1/

In order words, when parent checkbox is checked by clicking, the ng-model binds with the 3rd checkbox. But if the parent checkbox get checked due to ng-checked, the third checkbox is not updating the change. How can I achieve this in Angular?

Was it helpful?

Solution

No, I don't think it's possible without extra JS.

However, I made a version that does use a bit of JS and works fine.

<div><input type="checkbox" ng-click="clickOthers()" ng-model="childCheckbox" /> Child Checkbox</div>

(added to controller)

$scope.clickOthers = function () {
    $scope.parentCheckbox = $scope.childCheckbox;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top