最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Order by elements based on input text, AngularJS - Stack Overflow

matteradmin5PV0评论

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

my code is this

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

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

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

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

my code is this

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

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

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

Share Improve this question edited May 8, 2016 at 14:36 Juan Serrats 1,3585 gold badges24 silver badges31 bronze badges asked May 8, 2016 at 14:06 Antonio KidaAntonio Kida 538 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You have to define the variable in the controller:

var sort = '';

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="ments in dishDetailCtrl.dish.ments | orderBy: dishDetailCtrl.sort">

There is a very tiny mistake in your code. There should be no space after orderBy expression.

<li ng-repeat = "ment in dishCtrl.dish.ments | orderBy:sort">

If above doesn't work try, putting it in single quotes like 'sort'

You need bind an action on this input it can be simple ng-enter directive or a submit button.

Because you are using controller as syntax, as i can see from your ng-repeat, change

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

to

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

And also update your ng-repeat:

<li ng-repeat = "ment in dishCtrl.dish.ments | orderBy: dishCtrl.sort">
Post a comment

comment list (0)

  1. No comments so far