根据输入文本AngularJS按元素排序 [英] Order by elements based on input text, AngularJS

查看:58
本文介绍了根据输入文本AngularJS按元素排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据输入文本对anguarjs中的项目列表进行排序,因此,如果我在输入中输入作者",则该列表将由作者进行排序

I'm trying to order a list of items in anguarjs based on an input text, so if i write in my input "author" the list will be ordered by author

我的代码是这个

<input type = "text" ng-model = "sort">

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

它不起作用,我已经搜索过了,但是找不到这种示例.

It doesnt work, I already searched but can't find this kind of example.

推荐答案

您必须在控制器中定义变量:

You have to define the variable in the controller:

var sort = '';

然后,您必须定义ng-model指令以将输入值绑定到此变量:

Then you have to define the ng-model directive to bind the input value to this variable:

<input type="text" ng-model="dishDetailCtrl.sort">

然后您可以应用过滤器:

And then you can apply the filter:

<div class="well" ng-repeat="comments in dishDetailCtrl.dish.comments | orderBy: dishDetailCtrl.sort">

这篇关于根据输入文本AngularJS按元素排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆