1

I can't find a way to filter my list only by its title.

My list is an array made of objects like this :

{
  "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"
}

I also have an input field inside my html file

<input type="search" id="search" class="form-control" ng-model="search.title" placeholder="Search by title">

This field is being watched in my controller

$scope.$watch('search.title', function(val) {
      $log.log($filter('filter')(vm.posts, val));
    });

And that's where I want to filter my vm.posts (my list of the objects i mentioned above) only by its title. Instead, it is filtered by its whole object, both body and title part. I know how to do it in html file by filter:search, but I have no idea how to do this inside a controller.

1 Answer 1

1

It can be done in two way

$scope.$watch('search.title', function(val) {
    $log.log($filter('filter')(vm.posts, $scope.search);
});

OR

$scope.$watch('search.title', function(val) {
    $log.log($filter('filter')(vm.posts, {title: val});
});
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Exactly what I was looking for. The {title: val} works like a charm. Haven't thought about that and searched the whole internet for it... Big thanks!
@NotAJohnDoe np. Glad to help you. Thanks :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.