【问题标题】:Angularjs filter by certain elementAngularjs按某些元素过滤
【发布时间】:2016-04-05 21:38:32
【问题描述】:

我找不到仅按标题过滤列表的方法。

我的列表是一个由这样的对象组成的数组:

{
  "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(我上面提到的对象列表)的地方。相反,它被整个对象过滤,bodytitle 部分。我知道如何通过 filter:search 在 html 文件中执行此操作,但我不知道如何在控制器中执行此操作。

【问题讨论】:

    标签: javascript angularjs angularjs-filter angularjs-ng-model


    【解决方案1】:

    有两种方法

    $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});
    });
    

    【讨论】:

    • 太棒了!正是我想要的。 {title: val} 就像一个魅力。没想到这一点并在整个互联网上搜索...非常感谢!
    • @NotAJohnDoe np。很高兴能帮助你。谢谢:-)
    猜你喜欢
    • 2012-11-28
    • 2022-06-14
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多