0

Can someone help on how to re-write the below code in Angular. I am having problem in handling filters

ng-repeat="data in myController.data | filter:{filterFlag:'true'}"

Bunyamin Coskuner
  • 8,719
  • 1
  • 28
  • 48
Nancy
  • 911
  • 7
  • 26
  • 54
  • 1
    @Gary : this is Angular 1 !!! not 2 ! – mareks Mar 13 '18 at 11:18
  • @Nancy: take a look here : https://docs.angularjs.org/api/ng/filter/filter – mareks Mar 13 '18 at 11:20
  • I am not looking for any docs. I am looking for a solution. Also @mareks please read the question properly. I have asked how to re write the code in angular 4 . I am aware that the code is already in angular 1 – Nancy Mar 16 '18 at 14:40
  • Then you need to decide whether you want to use Angular or AngularJS. You cannot use ng-repeat with Angular; it is a directive provided by AngularJS. – mareks Mar 19 '18 at 18:33

1 Answers1

0

AngularJS

Lets say you have an array of objects (in your case named data) in your myController i.e.

this.data = [
{id: 1, name: 'Oscar', age: 36},
{id: 2, name: 'Nina', age: 36},
{id: 3, name: 'Alex', age: 39},
]

And you want to filter out people with the age 36, you could iterate over it in the template like this:

<div ng-repeat="data in myController.data | filter:{age:36}">
{{data}}
</div> 

Angular

In any case, filters are expensive. Thats why they are considered bad practice, thats why you are not recommended to use it in Angular. The style guide says, you ought to filter out the objects in the controller, rather than in the template. You might want to consider doing the same in AngularJS. You could write a method which does the job.

For more go to ... ng-repeat :filter by single field

mareks
  • 774
  • 6
  • 5