Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat with track by and filter and orderBy not working

I have this code.

http://jsfiddle.net/0tgL7u6e/

JavaScript

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.nameFilter = '';
    $scope.contacts = [
        {name: 'GHI'},
        {name: 'DEF'},
        {name: 'ABC'},
        {name: 'JKL'}
    ];
}

View

<div ng-controller="MyCtrl">
    <div><input type="text" ng-model="nameFilter" placeholder="Search..." /></div>
    <p ng-repeat="contact in contacts track by $index | filter: nameFilter | orderBy: name">{{ contact.name }}</p>
</div>

I don't know why the order is not working and why the filter is not working.

At another question, I've read about something that objects can't be filtered or ordered. But I have an array of the objects above. Also, it should work!?

What's the problem?

like image 602
PatrickB Avatar asked Apr 13 '15 14:04

PatrickB


People also ask

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

What can I use instead of NG-repeat?

The *ngFor directive in Angular is like the ng-repeat directive in AngularJS. It repeats the associated DOM element for each item in the specified collection.

How do you use two ng-repeat in a table?

NEST TWO ng-repeatThe first ng-repeat in the tr tag will create the rows and the second one in the td tag will create one column per element in the collection. By using one ng-repeat for the row and an other for the column, the rows and the columns of your table is now driven by a subset.

How does ng-repeat work?

Definition and Usage The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.


1 Answers

To use tracking with filters, the track by expression has to be added after the filter.

<p ng-repeat="contact in contacts | orderBy: 'name' | filter: nameFilter  track by $index">{{ contact.name }}</p>

Here is the working fiddle

like image 85
Sajeetharan Avatar answered Oct 07 '22 11:10

Sajeetharan