Angular,通过将属性作为文本输入进行排序

时间:2016-08-07 00:14:54

标签: angularjs angularjs-orderby

我想按不同的属性对评论列表进行排序,例如评分,日期,...... 但不使用按钮并在输入中将属性作为文本。

默认情况下,该属性是评级,但ng-repeat不会发布任何内容。

我的代码是:

<div ng-controller="controller as Ctrl">
       <p>SortBy: <input type="text" name="input" ng-model="Ctrl.dish.sortProperty"></p>
<blockquote ng-repeat="comment in Ctrl.dish.comments | orderBy:'{{Ctrl.dish.sortProperty}}'">
                        <p>{{comment.rating}}</p>
                        <p>{{comment.comment}}</p>
                        <footer>{{comment.author}} ,<cite title="Source Title">{{comment.date| date:'mediumDate'}}</cite></footer>
 </blockquote>
</div>

和控制器:

<script>

        var app = angular.module('myApp',[]); 
        app.controller('controller', function() {
            var dish={
                          sortProperty:'rating',
                           comments: [
                               {
                                   rating:5,
                                   comment:"blablalbla",
                                   author:"John Lemon",
                                   date:"2012-10-16T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"blablabla",
                                   author:"Paul McVites",
                                   date:"2014-09-05T17:57:28.556094Z"
                               },
                               {
                                // more comments
                               }
                           ]};
                    this.dish = dish; });
</script>

1 个答案:

答案 0 :(得分:0)

删除单引号和插值{{ }}

<blockquote ng-repeat="comment in Ctrl.dish.comments | orderBy: Ctrl.dish.sortProperty">