在使用多个阵列NG-重复 [英] Using ng-repeat on multiple arrays

查看:212
本文介绍了在使用多个阵列NG-重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个天气应用程序,但我卡在什么应该是一个简单的任务,但我不能换我围​​绕如何工作的,因为我是新来的角头。基本上,我需要的是一次遍历两个数组并从该数组设置一些值。

I'm making a weather application but I'm stuck on what should be a simple task but I can't wrap my head around how this works since I'm new to Angular. Basically what I need is to iterate over two arrays at once and set some values from this array.

在理论上说,这就是我想要做到:

In theory this is what I wanna accomplish:

<div ng-repeat="city in cities" ng-repeat="temp in temperatures" id={{city.id}}>
    <span>{{temp}}</span>
</div>

但显然你不能把多个中继器这样。如果我把一个 NG-重复上的跨度,我得到12跨度,但我只需要1,我需要这1包含在<$ C迭代的当前值$ C>温度。

But obviously you can't put multiple repeaters like this. If I put an ng-repeat on the span I get 12 spans but I only need 1 and I need this 1 to contain the current value of the iteration in the temperatures.

该阵列看起来像这样被动态创建使用API​​后:

The arrays looks like this after being created dynamically using an API:

城市: [对象,对象,目标,对象,目标,对象,目标,对象,目标,对象,目标,对象]

温度: [ - 2.3,-0.2,-1.2,-25.4,2.9,-4.8,-2.2,-12.1,0.3,-5.9,-7.7,-0.1]

我将如何做到这一点?

推荐答案

使用索引(假定温度是在同一个控制器定义下的数组):

Use an index (assuming temperatures is the array of temperatures defined in the same controller):

<div ng-repeat="city in cities">
    <span>City: {{city}} - Temperature: {{temperatures[$index]}}</span>
</div>

这篇关于在使用多个阵列NG-重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆