我是Angular JS的初学者,我正在尝试实现它来从一个dish数组中检索值以在视图页面中显示它。在某处,代码出错了,信息没有显示在视图页面中,尽管它出现在日志中。
我的HTML页面如下:
<div class="container" ng-controller="DishDetailController">
<div class="row row-content" ng-repeat="food in dish">
<div class="col-xs-12">
<div class="media" >
<div class="media-left media-middle">
<a href="#">
<img class="media-object"
ng-src={{food.image}} alt="Uthappizza">
</a>
</div>
<div class="media-body">
<h2 class="media-heading">{{food.name}}
<span class="label label-danger label-xs">{{food.label}}</span>
<span class="badge">{{food.price | currency}}</span></h2>
<p>{{food.description}}</p>
</div>
</div>
</div>
<div class="col-xs-9 col-xs-offset-1">
<h4>Customer comments       <small> Sort by <input type="text" ng-model="sortBy"></small></h4>
<ul class="list-unstyled">
<li>
<blockquote ng-repeat="commen in food.comments|orderBy:sortBy">
<p>{{commen.rating}} Stars</p>
<p>{{commen.comment}}</p>
<footer>{{commen.author}} <cite>{{commen.date| date:'mediumDate'}}</cite></footer>
</blockquote>
</li>
</ul>
</div>
</div>
和我的controller.js脚本在下面,它包含在body关闭标记之前的html页面中
.controller('DishDetailController', ['$scope', 'menuFactory', function($scope, menuFactory) {
$scope.sortBy="";
$scope.dish= menuFactory.getDish(3);
console.log($scope.dish);
}])
最后是services.js脚本,包含在正文关闭标记
之前'use strict';
angular.module('confusionApp')
.factory('menuFactory', function() {
var menufac = {};
var dishes=[
{ 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"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}]
},
{
name:'Zucchipakoda',
image: 'images/zucchipakoda.png',
category: 'appetizer',
label:'',
price:'1.99',
description:'Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce',
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"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}]
},
{
name:'Vadonut',
image: 'images/vadonut.png',
category: 'appetizer',
label:'New',
price:'1.99',
description:'A quintessential ConFusion experience, is it a vada or is it a donut?',
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"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}]
},
{
name:'ElaiCheese Cake',
image: 'images/elaicheesecake.png',
category: 'dessert',
label:'',
price:'2.99',
description:'A delectable, semi-sweet New York Style Cheese Cake, with Graham cracker crust and spiced with Indian cardamoms',
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"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}]
}
];
menufac.getDishes = function(){
return dishes;
};
menufac.getDish = function (index) {
return dishes[index];
};
return menufac;
});
答案 0 :(得分:0)
ng-repeat
指令需要一个数组:
<!-- ERRONEOUS
<div class="row row-content" ng-repeat="food in dish">
-->
<!-- DO THIS -->
<div class="row row-content" ng-repeat="food in [dish]">
<!-- OR -->
<div class="row row-content" ng-repeat="food in dishes">
答案 1 :(得分:0)
解决方案1:
如果你想要显示所有的菜肴,你必须给阵列重复:
在你的控制器中,用getDishes()代替getDishes(3):
.controller('DishDetailController', ['$scope', 'menuFactory', function($scope, menuFactory) {
$scope.sortBy="";
$scope.dish= menuFactory.getDishes();
console.log($scope.dish);
}])
也许你可以用菜重命名菜:
$scope.dishes= menuFactory.getDishes();
在html中:
ng-repeat = "food in dishes"
解决方案2:
如果您只想显示一个“菜”,只需删除ng-repeat =“菜肴中的食物”
答案 2 :(得分:0)
()