问题
JS
$scope.mode = "test"
$scope.array = ['test1', 'test2', 'test3']
HTML
<div ng-switch on="mode">
<div ng-repeat="item in array" ng-switch-when="test">
Test {{item}}
</div>
</div>
When I do this the above, the output I get is
Test Test Test
It can't seem to access item. If I remove the ng-switch-when then it works fine.
回答1:
It is because ng-switch
creates a child scope and same applies for ng-repeat. ng-repeat runs at priority 1000 and ng-switch
at 800. So the child scope created by ng-repeat
on each of the repeated div elements gets overwritten by ng-switch
, so the ng-repeat
assigned child scope on the element is gone ultimately. A possibly way could be to wrap your ng-switch outside ng-repeat since you anyways do not want to evaluate the ng-switch expression for every repeated element. This is with the 1.2 version.
Now from 1.3.x version of angular the priority of ng-switch has been revise to 1200 and ng-repeat remains at 1000, so ng-repeated scope will prevail, since it is of lower priority now and it will work in that case.
Plnkr
来源:https://stackoverflow.com/questions/25675742/ng-repeat-and-ng-switch-doesnt-work-on-same-element