简体   繁体   中英

AngularJS filter object array with search term in JavaScript

I´m currentrly trying to filter an array of objects with an search term from an input field. I know there is a way to apply a search term to an ng-repeat directive using this code ng-repeat="item in list| filter:search_term . But I need to process the filtered list in the JavaScript part of the application. Is there a way how to access the filtered list in the JS part of the application or do I have to choose another approach to filter my array by an search term?

Here is my (currently not working) example code.

EDIT:
I´m searching for a way to do the task completely without using a filter on my ng-repeat ! At the end it should be possible to display the filtered list by only using a simple ng-repeat="item in filtered_list

you can use filter in your controller also. Here is your updated fiddle. I hope this can help.

`https://jsfiddle.net/ymcfugzp/3/`

You should filter it inside the Component then.

You can have a function that filters and also does your other processing stuff , similar as the one that follows:

myFilter(term) {
  const filtered = list.filter(element => element.term === term);
  // Do other stuff in here.
  return filtered;
}

Then your template could be as simple as that:

ng-repeat="myFilter(term)"

You can do it like this: ng-repeat="item in filteredList = (list| filter:search_term) . And then just get your filteredList via $scope.filteredList .

Or use controller alias like this: ng-repeat="item in $ctrl.filteredList = ($ctrl.list| filter:search_term) - in order to not keep data in $scope

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