2016-08-07 22 views
0

我想根据不同的属性(如评级,日期等)对注释列表进行排序,但不使用按钮并将属性作为输入文本。Angular,按属性作为文本输入进行排序

默认情况下属性是评级,但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> 

回答

0

取下单引号和插值{{ }}

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

感谢,还有一个问题,能在ng模型或标记中的某处放置反向条件?就像我想要从高到低的评级显示和从低到高的评级显示,现在反之亦然 –

+0

嗯,你可以,但我建议你使用或单选按钮或复选框(真假)。你需要一些帮助吗? – developer033

+0

谢谢,但我的任务是使用输入文本。当然,你是正确的按钮使工作更容易 –

相关问题