Angularjs过滤器嵌套对象
我有像这样的角度嵌套对象。 有没有办法过滤嵌套属性Angularjs过滤器嵌套对象,angularjs,filter,angularjs-filter,Angularjs,Filter,Angularjs Filter,我有像这样的角度嵌套对象。 有没有办法过滤嵌套属性 <li ng-repeat="shop in shops | filter:search"> search.locations.city_id = 22 是的,如果我正确理解你的例子,你可以 根据集合的大小,最好计算在ng repeat中迭代的集合,这样过滤器就不会随着模型的变化而不断地进行过滤 基本上你是这样做的,如果我理解正确的话: $scope.search = function (shop) { if ($sc
<li ng-repeat="shop in shops | filter:search">
search.locations.city_id = 22
是的,如果我正确理解你的例子,你可以 根据集合的大小,最好计算在
ng repeat
中迭代的集合,这样过滤器就不会随着模型的变化而不断地进行过滤
基本上你是这样做的,如果我理解正确的话:
$scope.search = function (shop) {
if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
return true;
}
var found = false;
angular.forEach(shop.locations, function (location) {
if (location.city_id === parseInt($scope.selectedCityId)) {
found = true;
}
});
return found;
};
您也可以这样过滤(版本1.2.13+)
更新了“类似Jared的单词”的答案,以使用正则表达式检查它是否包含搜索词。这种方式在您键入1个数字时开始过滤,这样您就不必匹配整个单词
这是可行的,但默认情况下,结果不会仅在输入后显示@zajca您可以通过在控制器中为模型分配一个值来解决问题:
$scope.selectedCityId='
。这会在手动更改输入之前加载所有项。请注意,标准筛选器会将值作为从一开始就匹配的字符串进行比较。如果您请求city_id 1,它将返回位于id为10、15或12345的城市中的商店。此外,它将只搜索数组中的第一个对象,为了避免这种情况,您需要编写自己的比较器或过滤器。请参阅示例:-1:@Envek的评论“它将只在数组中的第一个对象中搜索”值得强调。使用您提供的小提琴,OP的示例搜索词(city_id=22)找不到结果,尽管类别为2的对象有一个具有该属性值的位置。
$scope.search = function (shop) {
if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
return true;
}
var found = false;
angular.forEach(shop.locations, function (location) {
if (location.city_id === parseInt($scope.selectedCityId)) {
found = true;
}
});
return found;
};
<li ng-repeat="shop in shops | filter: { locations: [{ city_id: search.locations.city_id }] }">
angular.forEach(shop.locations, function (location) {
if (checknum(location.city_id)) {
found = true;
}
});
function checknum(num){
var regx = new RegExp($scope.selectedCityId);
return regx.test(num);
};