简体   繁体   中英

AngularJs Filter Not Array in Nested Object

I have a nested JSON data. I am writing an object to a table by using ng-repeat from this JSON data. But I want to filter by a property this ng-repeat. How can I do this.

Sample:

<tr ng-repeat="i in inspections.analizFirst">
    <td>{{$index+1}}</td><td>{{i.analiz}}</td><td>{{i.poi_adi}}</td><td>{{i.mesafe}}</td>
</tr>

It works without filter. But I need to filter like ng-repeat="i in inspections.analizFirst | filter:{analiz:'ulasim analizi'}"

Use controller:

You can filter the data inside your controller and then refer to the filtered object in the html. It's usually more readable when you place all business logic in the controller

Create your own filter:

If you are planning to use this filter in multiple in the application, maybe its a good idea to create a new filter:

appModule.filter('myFilter', function() {
  return function(orginalList, arg1, arg2) {
    var filteredObject = [];
    orginalList.forEach(function(e) {
      if(e.analiz === arg1)
           filteredObject.push(e) 
    })
    return filteredObject;
   };
 })

html

<tr ng-repeat="i in inspections.analizFirst | myFilter:'ulasim analizi'">
  <td>{{$index+1}}</td><td>{{i.analiz}}</td><td>{{i.poi_adi}}</td><td>
 {{i.mesafe}}</td>
</tr>

Just like this:

<tr ng-repeat="i in inspections.analizFirst | filter:'ulasim analizi'">
    <td>{{$index+1}}</td><td>{{i.analiz}}</td><td>{{i.poi_adi}}</td><td>{{i.mesafe}}</td>
</tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM