Arrays 按属性筛选对象数组,整数值为ng repeat

Arrays 按属性筛选对象数组,整数值为ng repeat,arrays,angularjs,angularjs-ng-repeat,angularjs-filter,Arrays,Angularjs,Angularjs Ng Repeat,Angularjs Filter,我有这样的指令 ng-repeat="place in tb_places | filter:{'id':inputID}" $scope.tb_places = [ {name: 'some1', id:1} ... {name: 'some10', id:10} {name: 'some11', id:11} {name: 'some12', id:12} ... ] 输出某些对象数组的步骤如下所示 ng-repeat="place in tb_places | filter:{'id

我有这样的指令

ng-repeat="place in tb_places | filter:{'id':inputID}"
$scope.tb_places = [
{name: 'some1', id:1}
...
{name: 'some10', id:10}
{name: 'some11', id:11}
{name: 'some12', id:12} 
...
]
输出某些对象数组的步骤如下所示

ng-repeat="place in tb_places | filter:{'id':inputID}"
$scope.tb_places = [
{name: 'some1', id:1}
...
{name: 'some10', id:10}
{name: 'some11', id:11}
{name: 'some12', id:12} 
...
]
当我更改inputID并将其设置为1时,结果数组将填充“id”为1和10,11,12的源数组元素。因此,“id”值的部分像子字符串一样被检查,而不是数字。 我怎样才能治好它

谢谢

UPD 我尝试在过滤器表达式中添加“:true”-它完全清除输出(结果数组),它适用于简单的字符串数组,但不适用于对象(“true”希望与pattern对象完全匹配,它意味着与它的所有属性完全匹配)

解决了

对不起,伙计们,是我的错inputID与id(string vs int)的类型不同,因此内置比较器(“:true”)返回false。非常感谢

ps
对不起,我不能投票给你的答案-缺乏声誉。。。再见

您需要根据角度添加
比较器
,以达到您的要求

您能否将代码更改为:

ng-repeat="place in tb_places | filter: {'id' : inputID} : true"

您需要提供自己的比较器,或者手动将查询的值转换为整数,并使用
true
标志:

控制器:

app.controller('DemoCtrl', function() {
  this.query = '1';

  this.queryAsInt = function() {
    return parseInt(this.query, 10);
  };

  this.stuff = [
    {id: 1, label: 'Foobar 1'},
    {id: 2, label: 'Foobar 2'},
    {id: 3, label: 'Foobar 3'},
    {id: 4, label: 'Foobar 4'},
    {id: 5, label: 'Foobar 5'},
    {id: 6, label: 'Foobar 6'},
    {id: 7, label: 'Foobar 7'},
    {id: 8, label: 'Foobar 8'},
    {id: 9, label: 'Foobar 9'},
    {id: 10, label: 'Foobar 10'},
    {id: 11, label: 'Foobar 11'},
    {id: 11, label: 'Foobar 12'}
  ];

  this.compare = function(a, b) {
    return parseInt(a, 10) === parseInt(b, 10);
  };
});
视图:


使用自定义比较器:

  • {{item.label}
对于类型铸造:

  • {{item.label}
);

下面是一个运行示例:

编写自己的比较器

HTML


普朗克完整版

所以你想要一个精确的匹配?检查我尝试在过滤器表达式中添加的“:true”-它完全清除输出(结果数组),它适用于简单的字符串数组,而不是对象(“true”希望与pattern对象完全匹配,它意味着所有属性)我尝试在过滤器表达式中添加“:true”-它完全清除输出(结果数组),它适用于简单的字符串数组,而不是对象(“true”比较所有属性)。这不起作用,因为angular.equals不匹配,因为类型不匹配
filterId = function(actual, expected) {
    return actual == expected;
}