0

I am new to angular js so please forgive me for this basic question. I have an array of object literals and I want to add filter in angular js according to particular field type.

Array of object literals:

 var todos= [{text: 'To-DO1', done: false,group:'Group1' }, {text: 'To-do2', done: false, group:'Group2' } ];

I am using this code:

<select  data-ng-model="selectOption" data-ng-options="item as item.gtype for item in items"> </select>
<li data-ng-repeat="todo in todos track by $index | filter: selectOption.gtype ">

Above filter is not working

For populating the select I am using :

$scope.items = [                  
  {gtype:'Group1'},                         
  {gtype:'Group2'},`enter code here`
  {gtype:'Group3'},
  {gtype:'Group4'},
  {gtype:'Group5'}
];

Could someone please help me through ?

1 Answer 1

1

The problem is with the track by $index. The correct place for it is at the end of the expression, after the filter (see ngRepeat documentation)...

<li data-ng-repeat="todo in todos | filter: selectOption.gtype track by $index">

Live Demo

Edit: As requested, here's an updated fiddle to show an alternate message if filter produces nothing...

Live Demo

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

2 Comments

One more thing Anthony, If I want to display a message if nothing is present for that specific group then how can this be done ?
Check out the new fiddle I added to the answer. You can stash away the filtered results into a scope variable and use it in ngShow/ngHide.

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.