AngularJS: ng-if not working in combination with ng-click?

后端 未结 3 2126
长情又很酷
长情又很酷 2020-12-01 06:32

Given this test case using AngularJS 1.2 rc3: http://plnkr.co/edit/MX6otx (repeated below)

1.

  • ng-if toggle
  • 3条回答
    •  长情又很酷
      2020-12-01 07:03

      1. Why does 1 not work?: Because an ngIf defines its own scope, which prototypically inherits from its parent scope (just like ngRepeat). So, when you change the value of a field inside an ngIf, you change it in the ngIf scope, and not in its parent scope.
      2. Should 1 work?: No
      3. Why does 2 work?: Because in that case you modify the content of an object which is referenced by the ngId scope, through inheritance.
      4. Should 2 work?: Yes
      5. Can I rely 2 to work in future updates of AngularJS?: Why shouldn't you?

      This scope inheritance mechanism is explained very well in https://github.com/angular/angular.js/wiki/Understanding-Scopes

    提交回复
    热议问题