【问题标题】:orderyBy with ng-repeat not working带有 ng-repeat 的 orderyBy 不起作用
【发布时间】:2016-07-24 21:30:08
【问题描述】:

我正在尝试按答案之类的排序。我查看了 orderBy 的角度文档,看起来我正在关注它。但是,我的桌子不是按喜欢的人排序的。这是我的html。如果您希望我发布更多代码,请告诉我。

<div ng-controller="answerAndQuestionController"> 
    <h1 ng-bind='question.question'></h1>
    <h1 ng-bind='question.description'></h1>
    <table>
        <thead>
            <tr>
                <th>Answer</th>
                <th>Description</th>
                <th>User</th>
                <th>Likes</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat = "answer in answers | orderBy:'answer.likes'">
                <td><input ng-model = "answer.answer" readonly></td>
                <td><input ng-model = "answer.description" readonly></td>
                <td><input ng-model = "answer.user" readonly></td>
                <td><input ng-model = "answer.likes" readonly>
                    <button ng-click = "like(answer.answer)" href="">like</button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

【问题讨论】:

  • 尝试不使用answer,就像这样&lt;tr ng-repeat = "answer in answers | orderBy:'likes'"&gt;
  • 谢谢,把它贴出来,这样我就可以给你答案了

标签: html angularjs ng-repeat angularjs-orderby


【解决方案1】:

如果您查看 Angular 的 orderBy example,您会注意到在 orderBy 中定义表达式的类比没有“对象” (answer)

&lt;tr ng-repeat = "answer in answers | orderBy:'likes'"&gt;

【讨论】:

  • 过滤背后的想法是您已经拥有answer,现在您将管道这些变量进入过滤器,在这种情况下是orderBy过滤器跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多