javascript - AngularJS使用过滤器根据属性值查找特定项目

标签 javascript angularjs

我的情况是: 自定义过滤器返回 JSON 格式的 Product 类型的对象数组。每个产品对象都有 4-5 个属性,包括标题。

在 div 元素中,我想稍后使用标题为“Document Cloud”的产品的属性

基于这个答案How to filter by object property in angularJS我将 HTML 代码编写为:

<div ng-repeat="product in filteredTopProducts=((products | categoryProductsSearchFilter:searchData:{title:'Document Cloud'}))" class='top-product-icon' ng-click="productClicked(product)">

    <img class='top-product-icon-img' ng-src="{{product.thumbnail}}"/>
    <div class='top-product-icon-title' ng-bind="product.title"></div>
</div>

1) products 是过滤器的输入

2)categoryProductsSearchFilter是过滤器名称

3) searchData 是一个文本框模型名称 (ng-model),用户可以从中手动搜索和过滤产品

问题在于,上述内容并未过滤该特定产品,而是显示所有产品,这意味着 {title:'Document Cloud'} 无法正常工作。

最佳答案

您需要使用 ng-model 进行输入,它将按标题过滤您的对象。比在 ng-repeat 中只需按查询添加过滤器

<label for="filter">Filter by title</label>
<input type="text" ng-model="query.title">
<div ng-repeat="product in products | filter:query" ng-click="productClicked(product)">

指定要应用过滤器的属性

<input type="text" ng-model="queryTitle"> 
<div ng-repeat="product in products | filter:{ title: queryTitle }">

关于javascript - AngularJS使用过滤器根据属性值查找特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474693/

相关文章:

Javascript 比较数字给出错误答案

javascript - jQuery:如何检测由其他事件引起的单选按钮更改

Angularjs,使用 data-ng-controller 和 ng-controller

angularjs:ng-switch-when 中的多个值

angularjs - 在Angular中的ng-click上从所有子项中删除类,除了单击的子项

javascript - Controller 未加载

javascript - 如何在 grunt 中包含 javascript 库

javascript - 如何更改 chosen.js 中的警报语言?

javascript - 如何使用正则表达式匹配不包含多个特定单词之一的字符串?

javascript - 为什么 ngInfiniteScroll(无限滚动)不适用于移动设备?