我是 Angular 新手。我想要做的是显示页面上对象数组中的数据,并为用户提供在下一个/上一个数组项目上移动的按钮。
假设我们的数组是:
var destinations = [
{
name: "Rome",
weather: "sunny",
price: "$2999"
},
{
name: "Paris",
weather: "cloudy",
price: "$4500"
}
];
如果我想以 HTML 形式显示它,我可以编写 {{destinations[1].name}}
并且它可以工作,但是如何使其动态化?我尝试在 Controller 中定义 i
变量并将 [i]
插入 HTML 指令,但这不起作用。
我怎样才能做到这一点?
最佳答案
在 html 中使用 ng-repeat
<div ng-repeat="destination in destinations">{{destination}}</div>
或者在您可以使用的 Controller 中
angular.forEach(destinations,function(value) {
//here value is equal to one elment of your array
alert(value);
);
关于arrays - 如何在 Angular 中迭代数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37229567/