0

DISCLAIMER I'm coming from knockout so I'm having a little trouble switching my train of thought on how to do certain things, this being one of them...

I have an <input> that a user can enter a movie title in and a list of movies should be displayed. The collection of movies is served up from my rest api by calling http://www.example.com/api/movies?title=matrix. The api is wrapped in an angular-resource module.

I thought that $scope.movies = Movie.query({ title: $scope.searchValue }); would automatically update anytime $scope.searchValue changed but it doesn't appear to work that way.

angular.module('robotdog.resources', ['ngResource'])
    .factory('Movie', function($resource){
        return $resource('/api/movies/:id');
    });

robotdog.controller('HomeController', function($scope, Movie){
    $scope.movies = Movie.query({ title: $scope.searchValue });

});

Just for the sake of completeness here is my markup for this particular controller...

<input type="text" ng-model="searchValue" placeholder="Search Movie Title"/>
<pre>{{ movies | json }}</pre>

What would be the correct way to populate $scope.movies with the search result after a user has entered a value in the <input>?

2
  • are u getting response from server Commented May 3, 2013 at 17:27
  • if I type a value in the input and hit enter nothing happens. No request is sent and I get no error message in the console. Commented May 3, 2013 at 17:34

2 Answers 2

3

You're only calling Movie.query once, when the controller is initialized. You'll need to add a function to call Movie.query, and use that whenever the searchValue changes:

<input type="text" ng-model="searchValue" ng-change="getMovies" placeholder="Search Movie Title"/>


robotdog.controller('HomeController', function($scope, Movie) {
    $scope.getMovies = function() {
      $scope.movies = Movie.query({ title: $scope.searchValue });
    }
});

This will call your REST api whenever the user changes the input (i.e. on keypress), so if that causes too many RPCs you may want to add a 'search' button and only issue the RPC when it's hit:

<input type="text" ng-model="searchValue"/>
<button ng-click="getMovies">Go</button>
Sign up to request clarification or add additional context in comments.

1 Comment

FTFY: <button ng-click="getMovies()">Go</button>
0

Or you can try:

<input type="text" ng-model="searchValue" />
<p ng-repeat="movie in movies | filter: searchValue">{{ movie }}</p>

No JS required in controller, apart from a basic query:

$scope.movies = Movie.query();

Check out the fiddle: http://jsfiddle.net/euan/zuhga76c/

Comments

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.