【发布时间】:2014-07-05 07:04:49
【问题描述】:
学习ng-repeat的正常方式如下
<li ng-repeat="item in items">{{item.attribute}}</li>
你会有一个控制器,比如
app.controller('myCtrl', function($scope) {
$scope.items = [
{ name : kitten, attribute : value },
{ name : puppy, attribute : value }
];
});
这很好,但是当我深入了解我的应用程序并且必须通过数组索引引用项目时,它会变得很笨拙。修改小猫属性的函数会去:
$scope.items[0].attribute = value;
我更愿意:
app.controller('myCtrl', function($scope) {
$scope.items = {
'kitten' : {attribute : value },
'puppy' : { attribute : value }
};
});
这样我可以
$scope.items.kitten.attribute = NEW
-or-
$scope.items["kitten"].attribute = NEW
(are these equivalent?? I think so)
但是我将如何遍历它们呢?
<li ng-repeat="item in items">{{item.attribute}}</li>
不工作。
【问题讨论】:
-
除了tymeJV的回答,请养成经常查看the documentation的习惯。所有的答案都在那里。
-
文档很神秘。这里的答案总是有帮助的。我是 Angular 的新手。请收回你的DV。
标签: javascript angularjs model-view-controller