4

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 = "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.

4 Answers 4

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="comments in dishDetailCtrl.dish.comments | orderBy: dishDetailCtrl.sort">
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

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

2 Comments

thanks, i didnt think a space would cause my program not to run correctly
I faced this space issue a lot of times. Glad to help :)
0

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

Comments

0

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 = "comment in dishCtrl.dish.comments | orderBy: dishCtrl.sort">

1 Comment

Hi, it works now, thanks a lot, i tried it in that way and like the person below me said, thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.