我试图在angularjs中获得对象数组的精确关键匹配,而不是我得到所有匹配的结果。我怎样才能做到这一点?
这是控制器代码: -
var myApp = angular.module('myApp', []);
myApp.controller('ToddlerCtrl', function ($scope,$filter) {
$scope.toddlers = [
{
"name": "Toddler One",
"tid": "85",
},
{
"name": "Toddler Two",
"tid": "485",
},
{
"name": "Toddler Three",
"tid": "4485",
} ,
{
"name": "Toddler Four",
"tid": "8845",
}
];
var found = $filter('filter')($scope.toddlers, 85 );
console.log(found); // I want the exact match i.e. ("Toddler one" with "85" only) but I am getting 3 results.
});
答案 0 :(得分:3)
解决方案非常简单:添加一个true作为第三个参数,表明搜索需要精确。请注意,85不会匹配任何东西,你需要输入'85':
var found = $filter('filter')($scope.toddlers, '85' , true);
有关详细信息,请查看文档中的比较器参数:Angular Filter