javascript - Angular 过滤表达式

标签 javascript angularjs angularjs-filter ng-options angularjs-ng-options

我正在开发一个过滤器表达式,该表达式按元素的数据 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>

Working Plunkr

编辑

仅在文本框中输入值后启用过滤器,然后它将成为条件基础严格检查|过滤器:{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>

Updated Plunkr

关于javascript - Angular 过滤表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525343/

相关文章:

Javascript SQLite : SELECT statement with WHERE IN clause

javascript - 缩放到 PIXI.js 中的特定点

javascript - 检测用户是否从网页启用了 iOS 内容阻止

javascript - 当对象传递回 Controller 函数时,AngularJS 正在删除我的 $_parent 属性

html - Angular 错误 TS2531 : Object is possibly 'null'

javascript - AngularJS中显示的条件过滤列表

Angularjs 将数组作为参数传递给自定义过滤器

javascript - 如何将 JavaScript Unicode 范围正则表达式转换为 PHP PCRE?

node.js - 如何在 Protractor 测试中使用 module.exports 和 require?

javascript - Angular 过滤器将所有下划线替换为空格