【问题标题】:Angularjs ng-repeat, value array by keyAngularjs ng-repeat,按键值数组
【发布时间】:2016-11-01 03:54:48
【问题描述】:

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

 }]);

【问题讨论】:

  • @swen 你检查答案了吗
  • 不幸的是它不起作用。我更新了问题代码
  • 您是否在控制台中看到任何错误?
  • 不,没有错误
  • 我可以获取团队查看器吗?

标签: javascript angularjs arrays key ng-repeat


【解决方案1】:

改变,

 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;
});

如果你想遍历 cmets,视图应该是,

<blockquote ng-repeat="comment in dish[0].comments">
   <p>{{comment .rating}} Stars</p>
   <p>{{comment .comment}}</p>
   <footer>John Lemon</footer>
</blockquote>

DEMO

【讨论】:

  • 最好有一个内部 ng-repeat 来显示一个菜的所有 cmets 或 cmets 的子集。
猜你喜欢
  • 2016-10-04
  • 2018-01-28
  • 2016-02-12
  • 2015-04-23
  • 2015-12-19
  • 1970-01-01
  • 2015-10-23
  • 2015-06-27
  • 1970-01-01
相关资源
最近更新 更多