简体   繁体   English

AngularJs:如何对多个变量(例如ng-init)执行ng-repeat?

[英]AngularJs: how to do ng-repeat over multiple variables like ng-init can do?

With ng-init you can use multiple vars: 使用ng-init,您可以使用多个var:

ng-init="var1=value1; var2=value2"

I tried something similar with ng-repeat but it doesn't work 我用ng-repeat尝试了类似的方法,但是没有用

ng-repeat= "var1 in var1s; var2 in var2s"

Is there a way to do so with a correct syntax ? 有没有办法使用正确的语法呢?

Update: I want to use var1 for header and var2 for content of an accordion http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_accordion 更新:我想使用var1作为标题,使用var2作为手风琴的内容http://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_accordion

As others have said, ng-repeat only works over a single variable. 正如其他人所说, ng-repeat仅在单个变量上起作用。 But one workaround would be to use ng-repeat over a single object which has two keys, one for each variable you want to access. 但是一种解决方法是对具有两个键的单个对象使用ng-repeat ,每个要访问的变量一个。 The view might look like this: 该视图可能如下所示:

<div ng-repeat="data in dataObject">
    {{data.var1}} {{data.var2}}
</div>

In your controller, the scoped variable dataObject would have two (or more) keys, one called var1 and the other var2 . 在您的控制器中,作用域变量dataObject将具有两个(或更多)键,一个称为var1 ,另一个称为var2

No , There is no way you can use two ng repeats variable with a semi colon separation like ng-init. ,您无法使用带有ng-init之类的半冒号分隔的两个ng重复变量。

ng-repeat only works with single variable which is an array. ng-repeat仅适用于单个变量(即数组)。 If you want to have ng repeat for two different arrays, you could use nested-ng-repeat . 如果要对两个不同的数组重复执行ng,可以使用nested-ng-repeat

Otherwise merge your two arrays as one using key,value . 否则,使用key,value将两个数组合并为一个。 Then you will be able to do ng-repeat over key,value and access them. 然后,您将可以对键,值进行ng-repeat并访问它们。

DEMO USING NESTED NG-REPEAT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM