Angularjs 按特定元素过滤
Angularjs filter by certain element
我找不到仅按标题过滤列表的方法。
我的列表是一个由 object 组成的数组,如下所示:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
我的 html 文件中也有一个输入字段
<input type="search" id="search" class="form-control" ng-model="search.title" placeholder="Search by title">
我的控制器正在监视此字段
$scope.$watch('search.title', function(val) {
$log.log($filter('filter')(vm.posts, val));
});
这就是我想仅按标题过滤 vm.posts(我上面提到的 object 列表)的地方。相反,它被整个 object、body
和 title
部分过滤。我知道如何在 filter:search 的 html 文件中执行此操作,但我不知道如何在控制器中执行此操作。
可以通过两种方式完成
$scope.$watch('search.title', function(val) {
$log.log($filter('filter')(vm.posts, $scope.search);
});
或
$scope.$watch('search.title', function(val) {
$log.log($filter('filter')(vm.posts, {title: val});
});
我找不到仅按标题过滤列表的方法。
我的列表是一个由 object 组成的数组,如下所示:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
我的 html 文件中也有一个输入字段
<input type="search" id="search" class="form-control" ng-model="search.title" placeholder="Search by title">
我的控制器正在监视此字段
$scope.$watch('search.title', function(val) {
$log.log($filter('filter')(vm.posts, val));
});
这就是我想仅按标题过滤 vm.posts(我上面提到的 object 列表)的地方。相反,它被整个 object、body
和 title
部分过滤。我知道如何在 filter:search 的 html 文件中执行此操作,但我不知道如何在控制器中执行此操作。
可以通过两种方式完成
$scope.$watch('search.title', function(val) {
$log.log($filter('filter')(vm.posts, $scope.search);
});
或
$scope.$watch('search.title', function(val) {
$log.log($filter('filter')(vm.posts, {title: val});
});