bootstrap-angular.js сортировка комментариев по порядку

Я хочу отобразить комментарии в определенном порядке, введя дату, автора и рейтинг в разделе ввода. Они также должны иметь возможность сортировки по убыванию, например, если введена опция -date. Я застрял.

Сортировка комментариев

Вот мой код

                <ul class="list-unstyled" ng-repeat="comment in dishDetailCtrl.dish.comments | orderBy:dishDetailCtrl.sortBy">
                  <li><blockquote>
                      <h4>{{comment.rating}} Stars</h4>
                      <h4>{{comment.comment}}</h4>
                      <footer>
                          {{comment.author}}, {{comment.date | date:mediumDate}}
                      </footer>
                  </blockquote></li>
              </ul>

<script>

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

    app.controller('dishDetailController', function() {

        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"
                           },
                           {
                               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>

3 ответа

Решение

Выше код, кажется, работает, я думаю, что вы пропустили, чтобы написать ng-model неверное имя переменной ввода SortBy.

Так должно быть

ng-model="dishDetailCtrl.sortBy"

вместо

ng-model="sortBy"

HTML

<div ng-controller="dishDetailController as dishDetailCtrl">
  Sort By <input type="text" ng-model="dishDetailCtrl.sortBy">
  <ul class="list-unstyled" ng-repeat="comment in dishDetailCtrl.dish.comments | orderBy:dishDetailCtrl.sortBy">
    <li>
      <blockquote>
        <h4>{{comment.rating}} Stars</h4>
        <h4>{{comment.comment}}</h4>
        <footer>
          {{comment.author}}, {{comment.date | date:mediumDate}}
        </footer>
      </blockquote>
    </li>
  </ul>
</div>

Демо Планкр

<div class="col-xs-9 col-xs-offset-1">
                    <div class="container">
                        <div class="row">
                        <div class="col-xs-2">Customer Comments</div>
                        <div class="col-xs-3-offset-1">
                            Sort by<input type="text" ng-model="dishDetailControllerSortBy" value=" ">
                            </div>
                        </div>
                        <div class="media-list" ng-repeat="cmnt in dishCtrl.dish.comments | orderBy:dishDetailControllerSortBy">
                            <div class="media">
                            <blockquote>{{cmnt.comment}}

                            <footer>
                            {{cmnt.author}},{{cmnt.date | date:format:mediumDate}}
                                </footer></blockquote>
                            </div>


                        </div>

        </div>
    </div>
 <input ng-model="userInput"> </input> </p>
<blockquote ng-repeat="comment in dishCtrl.dish.comments | orderBy:userInput">
            {{comment.rating}} Stars
            {{comment.comment}}
     <footer>{{comment.author}} {{comment.date | date:'medium'}}</footer>
 </blockquote>
Другие вопросы по тегам