JohnDizzle
JohnDizzle

Reputation: 1348

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

Upvotes: 2

Views: 1041

Answers (3)

Arun Redhu
Arun Redhu

Reputation: 1579

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

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

Upvotes: 1

GProst
GProst

Reputation: 10237

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

Upvotes: 0

zurfyx
zurfyx

Reputation: 32807

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)"

Upvotes: 0

Related Questions