NG-重复和NG-开关不相同的元素上工作 [英] ng-repeat and ng-switch doesn't work on same element

查看:173
本文介绍了NG-重复和NG-开关不相同的元素上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

它似乎无法访问项目。如果删除了NG-开关时,那么它工作正常。

It can't seem to access item. If I remove the ng-switch-when then it works fine.

推荐答案

这是因为 NG-开关创建一个子范围,同样适用于NG-重复。 NG-重复的优先级运行 1000 NG-开关 800 。因此,通过 NG-重复每个重复的div元素创建的子范围得到由覆盖NG-开关,所以在元素的NG-重复分配的子范围,最终消失了。一个可能的方法可以是换NG重复以外的NG-开关,因为你反正不想评价NG-开关前pression每一个重复的元素。这是与1.2版

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.

从纳克开关角优先级的1.3.x版现在已经修改为 1200 并纳克重复保持在 1000 ,所以NG-反复范围将prevail,因为它是优先级较低的现在,也将努力在这种情况下。

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

Plnkr

这篇关于NG-重复和NG-开关不相同的元素上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆