0

I just started learning Angular and I am having trouble retrieving data based on a http-get request. It works when I simply retrieve all movies, but not when I try to retrieve movies based on a search term (cfr. search.html). I hope someone can tell me where I went wrong, I really can't see it. Thank you in advance.

app.js:

var app;

(function() {

  app = angular.module('imdb', ['ngRoute']);

  app.config(function($routeProvider) {
    $routeProvider
      .when('/search', {
        controller: 'SearchController',
        templateUrl: 'views/search.html' 
      })
      .when('/movies', {
        controller: 'MovieController',
        templateUrl: 'views/movies.html' //works fine
      })
      .otherwise({
        redirectTo: '/movies'
      });
  });
})();

SearchController.js

(function (app) {

  app.controller('SearchController', ['$http', '$scope', function ($http, $scope) {
    $scope.movies = [];

    $scope.searchMovie = function(title) {
      $http.get('https://angularbackend.azurewebsites.net/api/Movies/Search?title=' + title)
        .success(function(data) {
          $scope.movies = data;
        });
    };
  }]);
})(app);

search.html

<div>
  <form class="form" novalidate name="searchMovies" ng-submit="SearchController.searchMovie(title)" >
    <input type="text" ng-model="title" class="form-control" placeholder="enter title">
    <button type="submit" class="btn btn-primary btn-block">Search</button>
  </form>
  <table class="table">
    <thead>
    <tr>
      <th>poster</th>
      <th>title</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="movie in movies">
      <td>{{ movie.title }}</td>
    </tr>
    </tbody>
  </table> 
</div>
2
  • Have you tried pasting https://angularbackend.azurewebsites.net/api/Movies/Search?title=someTitle into the browser to make sure your backend is responding correctly? Commented Apr 18, 2016 at 15:58
  • What excatly is not working? Do you get wrong data? Or is your APi call just not fired, do you get a 404 or is your title empty when making the call... ? Commented Apr 18, 2016 at 16:00

1 Answer 1

1

Replace

SearchController.searchMovie(title)

by

searchMovie(title)

All expressions are always evaluated on the scope. So the first, incorrect one, will try to invoke the method searchMovie of $scope.SearchController, which doesn't exist.

Also note that success() is deprecated for quite some time now. Use then():

$http.get('https://angularbackend.azurewebsites.net/api/Movies/Search?title=' + title)
    .then(function(response) {
      $scope.movies = response.data;
    });

You should also avoid using string concatenation to pass parameters. Those need to be encoded properly. So rather use

$http.get('https://angularbackend.azurewebsites.net/api/Movies/Search', {params: {title: title}})
    .then(function(response) {
      $scope.movies = response.data;
    });
Sign up to request clarification or add additional context in comments.

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.