Angularjs ng-repeat,按键排列值

时间:2016-10-31 18:18:14

标签: javascript angularjs arrays key ng-repeat

请帮我解决。我无法获得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;

 }]);

1 个答案:

答案 0 :(得分:2)

更改,

 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>

<强> DEMO