[英]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?我知道有一种方法可以使用此代码
ng-repeat="item in list| filter:search_term
将搜索词应用于 ng-repeat 指令。但我需要在应用程序的 JavaScript 部分处理过滤列表。是有没有一种方法可以访问应用程序的 JS 部分中的过滤列表,或者我是否必须选择另一种方法来通过搜索词过滤我的数组?
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
!我正在寻找一种完全完成任务的方法,而无需在我的
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
最后,应该可以只使用一个简单的
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.然后你应该在
Component
内部过滤它。
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
.你可以这样说:
ng-repeat="item in filteredList = (list| filter:search_term)
然后,只是让你filteredList
通过$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
或者像这样使用控制器别名:
ng-repeat="item in $ctrl.filteredList = ($ctrl.list| filter:search_term)
- 为了不在$scope
保留数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.