1

I have an array of data that gets populated dynamically via a socket event. However I can't sort the array properly with angular's ng-repeat:

socket.on('twitter:item', function(data){
    var item = data;

    item.time = moment(data.time).valueOf();
    item.date = formatTime(data.time);

    $scope.twitter.push(item);
});


<li ng-repeat="item in twitter | orderBy:time:true">
    <a href>{{item.title}}</a> - {{item.time}} {{item.date}}
</li>

The html always shows the most recent at the bottom of the ordered list...I want it at the top (thus the orderBy:time:true (or false) where time is milliseconds. Neither of which work.

4
  • What's the value of model 'reverse'? Commented Dec 19, 2013 at 7:19
  • i messed up reading the docs. but it is either true or false (updated post) Commented Dec 19, 2013 at 7:20
  • 1
    Try <li ng-repeat="item in twitter | orderBy:'time':true"> Commented Dec 19, 2013 at 7:26
  • that was it. make that the answer and i'll accept. Commented Dec 19, 2013 at 7:28

1 Answer 1

2

Try <li ng-repeat="item in twitter | orderBy:'time':true">.

orderBy accepts an expression string as its first argument. It will be evaluated to a value.
And time will be evaluated as undefeind since it's not existed in scope.

Use single quotation to make 'time' evaluated as string.

Sign up to request clarification or add additional context in comments.

Comments

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.