在多个数组上使用 ng-repeat [英] Using ng-repeat on multiple arrays

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

问题描述

我正在制作一个天气应用程序,但我被困在什么应该是一个简单的任务上,但我无法理解这是如何工作的,因为我是 Angular 的新手.基本上我需要的是一次迭代两个数组并从这个数组中设置一些值.

理论上这就是我想要完成的:

<span>{{temp}}</span>

但显然你不能像这样放置多个中继器.如果我在跨度上放置一个 ng-repeat,我会得到 12 个跨度,但我只需要 1 个,我需要这个 1 来包含 温度 中迭代的当前值.

使用 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]

我该怎么做?

解决方案

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

<span>城市:{{city}} - 温度:{{temperatures[$index]}}</span>

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>

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.

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

Cities: [object, object, object, object, object, object, object, object, object, object, object, object]

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

How would I do this?

解决方案

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-repeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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