如果你看看下面的代码,我想使用文本<input>
来过滤每个菜单项的多个成分 - 例如,如果用户在<input>
中输入“牛肉,培根”,应用程序将返回所有与牛肉或培根作为配料的菜单项目。AngularJS过滤 - 多个表达式或动态链接过滤器?
我目前正在尝试使用ng-filter来做到这一点,我猜我需要为此创建一个自定义过滤器。这是错误的做法?有没有办法动态链式过滤器呢?
下面是一些代码,应该让我的问题意识 -
我的搜索模式: - 注意:使用NG列表把字符串转换成字符串数组
<div ng-init="searchString=[]">
<input type="text" ng-model="searchString" ng-list>
</div>
我的重复循环: - 注意:使用自定义过滤器将每个成分加入到一个字符串中
<tr ng-repeat="item in menu | filter:{ category : 'Classics' } | filter:{ ingredients : searchString } ">
<td class="title">{{ item.title }}</td>
<td class="ingredients">
{{ item.ingredients | join:', ' }}
</td>
<td class="price">{{ item.price | currency }}</td>
</tr>
我的数据结构
$scope.menu = [
{
"title" : "New Yorker",
"price" : "4.00",
"ingredients" : [
"Salt Beef",
"Pickles",
"Mustard"
],
"category" : "Classics"
},
{
"title" : "BLT",
"price" : "4.00",
"ingredients" : [
"Bacon",
"Lettuce",
"Tomato"
],
"category" : "Classics"
}
]