ng-repeat on two arrays

坚强是说给别人听的谎言 提交于 2019-12-04 00:39:52

问题


I want to do a ng-repeat on an array which is composed of two arrays, like this :

[titles: [], links: []]

My arrays (titles and links) have the same length What i want to print in my ng-repeat, finally, is anything like that :

{{ array.title }}
{{ array.link }}

For example, in a C program i have to do that :

int i;

i = 0;
while (titles[i])
{
    printf("%s - %s", titles[i], links[i]);
    i++;
}

回答1:


It's not clear how you have your data — [titles: [], links: []] isn't meaningful.

Ideally you would arrange your data as an array of objects that looks like:

var array = [{title: "foo", link: "bar"}, {title…etc]

Then you could just use:

<div ng-repeat="obj in array">
   {{obj.title}},  {{obj.link}}

If you know that titles and links will always be the same size you can do this, but it's not very pretty and might be a little fragile:

<div ng-repeat="title in titles">
   {{title}}, {{links[$index]}}
</div>



回答2:


This is how it worked for me:

<ion-item ng-repeat="name in product.names">
    Buy {{name}} for {{product.prices[$index]}}
</ion-item>

I had both product.names and product.prices of same size




回答3:


If the length is the same, then make it an object rather than two arrays.

plunker

<ui ng-repeat="movie in movies">
  <li>{{movie.t}} located in {{movie.l}}</li>
</ui>


来源:https://stackoverflow.com/questions/25109901/ng-repeat-on-two-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!