ngIf breaks ngModel #7216
Comments
You can read up more on the scope-related topics here: |
Hm, OK, but this doesn't look right. Basically The fact that it creates a new isolated child scope is a side-effect. You use ngIf to add / remove elements from DOM, not to isolate scope. Maybe this should be given more thought. Also, thank you for your answer. |
@Francisc this is not only about ngIf but you are going to see the same behaviour with any directive that is creating a child scope (btw, those are not isolated scopes): ngRepeat, ngView etc. Those scopes serve a purpose as then we can clean up model associated with a given scope. If you remove part of the DOM you don't want to keep model specific to this part of the view, right? |
That's true, I don't want to leave suspended scope data, but I still think it should be able to compile in the same context. Another issue for me is the fact that, for example, in the Plunkr above, the In other news, why doesn't GitHub notify me about comments to this issue?... |
The fact that For example, when I use the
This is a huge problem in some cases. For example, due to the use of Code example: <!-- index.html: I made this. -->
<div ng-controller="ParentController">
<div ng-include="templateMadeByDesigner.html"></div> <!-- New scope -->
</div>
<!-- templateMadeByDesigner.html: Some other designer made this. -->
<div ng-if="isSomething"> <!-- New scope -->
<span>blah blah</span>
<!-- number of ngIf directives used here is unknown only to the designer, not me. It's supposed to be just "presentation logic" right? -->
<div ng-if="someBoolean"> <!-- New scope -->
<span>lorem ipsum</span>
<div ng-controller="ChildController"> <!-- finally, the template designer has declared the required child ngController. -->
...
</div>
</div>
</div> In this example, common intuition (for those who don't already know about The mere fact that However, creating a child So how can we come to a middle ground? Perhaps a scope's It is highly likely that a developer will want to simply access the scope of the nearest parent |
I edited my previous comment. If you read replies by email, please read it on GItHub instead. Cheers. |
If the solution to this problem won't be fixed in AngularJS itself, a simple workaround would be to create a circular assignment of a controller's scope to make the scope easily accessible in a child scope regardless of how many Considering the HTML code in my previous comment, this would be the parent controller app.controller("ParentController", function($scope) {
$scope.myParentScope = $scope; // circular reference so children scopes can access this easily.
}); In the child controller you can access the parent scope: app.controller("ChildController", function($scope) {
console.log($scope.myParentScope); // logs the parent scope.
// no longer need to do $scope.$parent.$parent.$parent.$parent
}); That's a simple solution, but it will only work if you as the developer have access to both the ParentController and ChildController. There might be situations where you are only allowed to develop the child controller, and thus an Angular-specific fix would be nice. |
Hello,
If, for example, you place a checkbox component inside a container with
ngIf
, inside thengChange
method, the state of the checkbox is always false.I've made a simple Plunkr to demonstrate this.
http://plnkr.co/edit/QEIXZR1kJVvcw30R1O0t?p=preview
The text was updated successfully, but these errors were encountered: