我正在处理一个过滤器表达式,该表达式按元素的数据ID进行过滤。
我遇到的问题是,如果其他ID位于其他位置,则过滤器将返回多个值(即过滤器 12 RETURNS 12 , 12 3, 12 34)。我想只返回一个元素,其精确值为12.
JS
angular.module('app', []).controller('TestCtrl', function($scope) {
$scope.customers = [
{
id: 12,
name: 'customer 1 - 1'
},
{
id: 123,
name: 'customer 2 - 12'
},
{
id: 1234,
name: 'customer 3 - 12'
}
];
});
HTML
Filter: <input ng-model="filterCustomer" />
<hr/>
<select size="4" ng-options="customer as customer.name for customer in customers | filter:{id: filterCustomer} track by customer.id" ng-model="customerSelection">
</select>
我也尝试将以下表达式添加到过滤器以匹配数字长度...但它没有按预期工作
filter:{id:filterFsaisCustomer.toString().length}
Here是一名普通人。
答案 0 :(得分:1)
应该通过在过滤器中添加true
来进行严格检查,您也应该只需将integer
的输入字段解析为{type="number"
,就可以转换int
中的值{1}}。
<强>标记强>
<input type="number" ng-model="filterCustomer" />
<select size="4"
ng-options="customer as customer.name for customer in customers | filter:{id: filterCustomer}:true track by customer.id"
ng-model="customerSelection">
</select>
修改强>
要仅在文本框中输入值后启用过滤器,它才会成为条件基础严格检查| filter:{id: filterCustomer}:filterCustomer>0
<select size="4"
ng-options="customer as customer.name for customer in customers | filter:{id: filterCustomer}:filterCustomer>0 track by customer.id"
ng-model="customerSelection">
</select>