javascript - 使用 ngRepeat 指令循环时生成多个标签

标签 javascript angularjs

我知道 ngRepeatforEach 但我需要的或多或少是这两者的混合体。我的意思是:

我在 $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/

相关文章:

javascript - 在 Kendo Angular DatePicker 中禁用 future 日期

angularjs - Karma 中服务调用成功响应的代码覆盖率

javascript - Leaflet GeoJSON 图层控件在此脚本中不起作用?

JavaScript 和麦克风音频流

javascript - 将变量发送到作用域

javascript - 当超链接没有 href 时,如何导航到超链接所在的页面?

javascript - 带有 Protractor/WebDriverJS 元素的 Jasmine 自定义匹配器

javascript - 如何在 Vue.js 中获取对象数组中的键

javascript - 将相同的参数应用于函数列表,并在每个结果之间执行操作

angularjs - Angular 指令检查元素?