Angularjs ng-repeat,按键值数组
Angularjs ng-repeat, value array by key
请帮我理解一下。我无法获取 ng-repeat 的键值数组(在 blockquote 中)。没有显示任何内容,控制台中也没有错误。
这是我更改后的代码:
<div class="row row-content" ng-controller = "DishDetailController">
<blockquote ng-repeat="comment in dish">
<p>{{comment.rating}} Stars</p>
<p>{{comment.comment}}</p>
<footer>John Lemon</footer>
</blockquote>
</div>
脚本:
angular.module('confusionApp', [])
.controller('DishDetailController', ['$scope', function($scope) {
var dish=[{
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comment: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
]
}
];
$scope.dish = dish;
}]);
改变,
this.dish = dish;
至
$scope.dish = dish;
并将 $scope 变量注入你的控制器,
var app = angular.module('confusionApp', []);
app.controller('dishDetailController', function($scope) {
var dish = [{
name: 'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label: 'Hot',
price: '4.99',
description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
}, {
rating: 4,
comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
}]
}];
$scope.dish = dish;
});
如果你想遍历评论,视图应该是,
<blockquote ng-repeat="comment in dish[0].comments">
<p>{{comment .rating}} Stars</p>
<p>{{comment .comment}}</p>
<footer>John Lemon</footer>
</blockquote>
请帮我理解一下。我无法获取 ng-repeat 的键值数组(在 blockquote 中)。没有显示任何内容,控制台中也没有错误。 这是我更改后的代码:
<div class="row row-content" ng-controller = "DishDetailController">
<blockquote ng-repeat="comment in dish">
<p>{{comment.rating}} Stars</p>
<p>{{comment.comment}}</p>
<footer>John Lemon</footer>
</blockquote>
</div>
脚本:
angular.module('confusionApp', [])
.controller('DishDetailController', ['$scope', function($scope) {
var dish=[{
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comment: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
]
}
];
$scope.dish = dish;
}]);
改变,
this.dish = dish;
至
$scope.dish = dish;
并将 $scope 变量注入你的控制器,
var app = angular.module('confusionApp', []);
app.controller('dishDetailController', function($scope) {
var dish = [{
name: 'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label: 'Hot',
price: '4.99',
description: 'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [{
rating: 5,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
}, {
rating: 4,
comment: "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
}]
}];
$scope.dish = dish;
});
如果你想遍历评论,视图应该是,
<blockquote ng-repeat="comment in dish[0].comments">
<p>{{comment .rating}} Stars</p>
<p>{{comment .comment}}</p>
<footer>John Lemon</footer>
</blockquote>