Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ngRepeat指令循环时生成多个标记_Javascript_Angularjs - Fatal编程技术网

Javascript 使用ngRepeat指令循环时生成多个标记

Javascript 使用ngRepeat指令循环时生成多个标记,javascript,angularjs,Javascript,Angularjs,我知道ngRepeat和forEach,但我需要的或多或少是这两者的混合体。我的意思是: 我在$scope中有一个列列表。我可以用 <th ng-repeat="col in columns">{{ col.label }}</th> 有没有一种方法可以使用ng repeat,这样我就可以同时呈现config和label列?也许像 <repeat for="col in columns"> <th ng-show="mode == 'admin'"

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

我在
$scope
中有一个列列表。我可以用

<th ng-repeat="col in columns">{{ col.label }}</th>
有没有一种方法可以使用
ng repeat
,这样我就可以同时呈现config和label列?也许像

<repeat for="col in columns">
  <th ng-show="mode == 'admin'">config</th>
  <th>{{ col.label }}</th>
</repeat>

配置
{{col.label}}

或者,我唯一的选择是创建一个已经有管理列的新列表,并在每次更改
模式时重新生成(使用
forEach
)?最好的方法是什么?

最好使用返回列的函数

$scope.getColumns = function() {
  if (mode != 'admin') {
    return columns;
  } else {
    var c = new Array(columns.length * 2);
    for (var i=0; i< c.length; i++) {
      c[i] = i % 2 == 0 ? 'config' : columns[parseInt(i/2)];
    }
    return c;
  }
}
$scope.getColumns=function(){
如果(模式!=“管理”){
返回列;
}否则{
var c=新数组(columns.length*2);
对于(变量i=0;i
模板

<th ng-repeat="column in getColumns()" ng-bind="column"></th>

您可以尝试使用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>
{{foo.something}
这里有什么吗
此代码将重复,应该可以正常工作

有关更多信息,请参阅。

您是否尝试过使用
ng show
ng if
?@DevlshOne将
元素包装到div中,这不是有效的html。除非您的配置和列是相同长度的数组,否则如果不首先映射控制器中的数据,我看不出如何完成此操作。使用示例创建plunker演示data@charlietfl谢谢我现在要睡觉了,明天我将制作一个plunker演示。谢谢你的建议。实际上,在我的代码中,我使用了一个类似于您的函数,因为我需要过滤掉一些列。我的问题是关于
ngRepeat
的更一般的问题,这就是为什么我在这里使用列中的
列,而不是getColumns()中的
列。
。谢谢,我错过了文档中的那部分。我在搜索使用示例,并快速向下滚动页面,而没有查看文档中的其他详细信息。如果你需要更多的帮助,请随时联系我。
<th ng-repeat="column in getColumns()" ng-bind="column"></th>
  <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>