2

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

1
  • You can pass the object from AngularJS to Javascript by adding it to window. Ex: window.sorted = vm.sortedData; Commented Jan 23, 2017 at 10:00

3 Answers 3

1

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

`https://jsfiddle.net/ymcfugzp/3/`
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I searched for this kind of solution dealing with the filter in the JS Part of the application!
0

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

1 Comment

Can you please provide me a little working code example?
0

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

Comments

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.