gpt4 book ai didi

ng-switch 中的 AngularJS $index

转载 作者:行者123 更新时间:2023-12-03 22:25:36 25 4
gpt4 key购买 nike

我正在尝试访问 $indexng-repeat并将其放入 ng-switch-when . Here is the last thing I have tried .我正在尝试做的一小部分:

<li ng-repeat="contact in contacts" ng-switch on="MenuSelected">
<span ng-switch-when="$index">{{ contact }} {{$index}}</span>
</li>
MenuSelected例如,我在我的 Controller 中定义了 0,并且我有 2 个用于测试的联系人。我试过混合 ng不同标签中的东西和所有东西都掉下来(只显示线条),所以我不认为这是问题所在。当我删除 $index并放置例如 0 一切都显示出来。

最佳答案

您不能在 ngSwitch 中使用表达式

引用此 doc

Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal



这意味着 ng-switch-when="$index" $index被视为字符串而不是索引值

改用 ngIf
    <span ng-if="$index == MenuSelected">{{ contact }} {{$index}}</span>

这是工作 Fiddle

关于ng-switch 中的 AngularJS $index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27144998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com