我知道 ngRepeat
和 forEach
但我需要的或多或少是这两者的混合体。我的意思是:
我在 $scope
中有一个列列表。我可以用
<th ng-repeat="col in columns">{{ col.label }}</th>
那会呈现
<th>Col A</th>
<th>Col B</th>
<th>Col C</th>
在 $scope
中,我有一个变量 (mode
)。当此变量的值为 admin
时,将使用 ng-show
显示一些与 admin 相关的 html 标记。此外,当此变量为 admin
时,我希望我的列能够像这样呈现
<th>config</th>
<th>Col A</th>
<th>config</th>
<th>Col B</th>
<th>config</th>
<th>Col C</th>
有没有办法以某种方式使用 ng-repeat
以便我可以同时呈现配置和标签列?也许像
<repeat for="col in columns">
<th ng-show="mode == 'admin'">config</th>
<th>{{ col.label }}</th>
</repeat>
或者我唯一的选择是创建一个已经有管理列并在每次 mode
更改时重新生成(使用 forEach
)的新列表?最好的方法是什么?
最佳答案
您可以尝试使用 ng-repeat-start 和 ng-repeat-end。像这样:
<div ng-repeat-start="foo in foos">{{ foo.something }}</div>
Anything in here
<div><div ng-repeat-end="">This code will repeat and should work</div>
</div>
引用https://docs.angularjs.org/api/ng/directive/ngRepeat获取更多信息。
关于javascript - 使用 ngRepeat 指令循环时生成多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296784/