-2

我想通过使用“ng-repeat”和“ng-controller”显示所有评论元素,如列表,但我不知道如何在菜中显示评论元素!像这样:

5 星
想象一下所有的食物,生活在混乱中!
约翰柠檬,十月。17,2012

4星
送人上天堂,好想让婆婆吃!
Paul McVites,2014 年 9 月 6 日
。. .

    var app = angular.module('confusionApp',[]);

    app.controller('dishDetailController', function() {
      this.filtText= '';
      var dish=[
                  {
                    name:'Uthapizza',
                    image: 'images/uthapizza.png',
                    category: 'mains',
                    lable:'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"
                         }

                     ]
              }];

      this.dish = dish;


    });

    </script>
4

3 回答 3

2

笔记:

由于dish是一个数组,按照惯例,它应该是复数的,所以我做到了。

如果您只想在第一道菜的评论中进行迭代,它可以:

var app = angular.module('confusionApp', []);

app.controller('dishDetailController', function() {
  this.filtText = '';
  var dishes = [{
    name: 'Uthapizza',
    image: 'images/uthapizza.png',
    category: 'mains',
    lable: '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"
      }
    ]
  }];

  this.dishes = dishes;
});
<!DOCTYPE html>
<html ng-app="confusionApp">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="dishDetailController as ctrl">
  <div ng-repeat="c in ctrl.dishes[0].comments">
    <span ng-bind="c.rating + ' stars'"></span><br>
    <span ng-bind="c.comment"></span><br>
    <span>{{c.author}}, {{ c.date | date: 'MMM. dd,yyyy' }}</span><p>
  </ul>
</body>

</html>

但是您也可以使用特殊的重复次数来迭代所有菜肴,如下所示:

var app = angular.module('confusionApp', []);

app.controller('dishDetailController', function() {
  this.filtText = '';
  var dishes = [{
    name: 'Uthapizza',
    image: 'images/uthapizza.png',
    category: 'mains',
    lable: '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"
      }
    ]
  }];

  this.dishes = dishes;
});
<!DOCTYPE html>
<html ng-app="confusionApp">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="dishDetailController as ctrl">
    <div ng-repeat-start="d in ctrl.dishes" ng-bind="d.name"></div><br>
    <div style="margin-left: 10px" ng-repeat-end ng-repeat="c in d.comments">
      <span ng-bind="c.rating + ' stars'"></span><br>
      <span ng-bind="c.comment"></span><br>
      <span>{{c.author}}, {{ c.date | date: 'MMM. dd,yyyy' }}</span><p>
    </div>
</body>

</html>

有关日期中使用的过滤器的参考,您可以在此处查看

于 2016-07-06T14:50:48.287 回答
0

这是一个简单的 HTML,仅当您的菜数组包含 1 个元素时才有效。如果它包含多个元素,您可以简单地遍历它

HTML

<html ng-app="confusionApp">
<body>
    <div ng-controller="dishDetailController">
        <ul ng-repeat="c in dish[0].comments">
            <li>{{c.rating}} stars</li>
            <li>{{c.comment}}</li>
            <li>{{c.author}}, {{c.date | date: 'mediumDate' }}</li>
        </ul>
    </div>
</body>
</html>

您可以<ul>通过任何其他标签更改标签以微调您要查找的内容

于 2016-07-06T14:45:25.093 回答
0
<ul class="list-unstyled">
            <li ng-repeat="comment in dish.comments | orderBy:query">
                <blockquote>
                    <p> {{comment.rating}} stars</p>
                    <p>{{comment.comment}}</p>
                    <footer>{{comment.author}} ,{{comment.date | date:'MMM,dd,yyyy'}}</footer>
                </blockquote>
            </li>
        </ul>
于 2017-01-07T13:49:22.583 回答