我正在开发一个过滤器表达式,该表达式按元素的数据 ID 进行过滤。
我遇到的问题是,如果其他 ID 在其他位置具有相同的数字(即过滤器 12 RETURNS 12、 123、1234)。我只想返回精确值为 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是一个笨蛋。
最佳答案
应该通过在过滤器中添加 true
来进行严格检查,您还应该通过将输入字段设置为 type 来将
会将其解析为 integer
中的值转换为 integer
="number"int
。
标记
<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>
编辑
仅在文本框中输入值后启用过滤器,然后它将成为条件基础严格检查|过滤器:{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>
关于javascript - Angular 过滤表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525343/