TypeError:无法读取未定义的属性'jsonp'

时间:2015-11-04 10:33:47

标签: angularjs

我创建了一个将数据保存在数据库中的函数。

  $scope.addMovie = function() {

    'http://api.themoviedb.org/3/movie/206647?api_key=a8f7039633f2065942cd8a28d7cadad4&append_to_response=releases'
    // Search for release dates using the ID.
    var base = 'http://api.themoviedb.org/3/movie/';
    var movieID = $(event.currentTarget).parent().find('.movieID').text()
    var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
    var append_to_response = '&append_to_response=releases'
    var callback = 'JSON_CALLBACK'; // provided by angular.js
    var url = base + movieID + '?api_key=' + apiKey + append_to_response + '&callback=' + callback;

    $http.jsonp(url,{ cache: true}).
      success(function(data, status, headers, config) {

        if (status == 200) {

          $scope.movieListID = data;
          console.log($scope.movieListID);

          var releaseNL;

          for (var i = 0; i < $scope.movieListID.releases.countries.length; i++) {
            var release = $scope.movieListID.releases.countries[i];
            if (release['iso_3166_1'] == 'NL') {
                releaseNL = release;
            }
          }

          if(typeof releaseNL === 'undefined'){
            // With release date

            Notification($scope.movieListID.original_title + ' is toegevoegd, maar heeft nog geen Nederlandse premiere datum.');

            createMovie.create({
              title:          $scope.movieListID.original_title,
              release_date:   $scope.movieListID.release_date,
              image:          $scope.movieListID.poster_path,
              movie_id:       $scope.movieListID.id
            }).then(init);

          } else {
            Notification.success($scope.movieListID.original_title + ' is toegevoegd.');

            createMovie.create({
              title:          $scope.movieListID.original_title,
              release_date:   releaseNL.release_date,
              image:          $scope.movieListID.poster_path,
              movie_id:       $scope.movieListID.id
            }).then(init);
          };

        } else {
            console.error('Error happened while getting the movie list.')
        }
      })

      $(".search_results").fadeOut(250);

      $scope.searchquery = null
  };

我想从控制器中删除此代码以进行维修。所以我创建了一个“addMovieService.js”

(function(){
  "use strict";

  angular.module('addMovieseat')

  .factory('addMovie', [

    function($http, $q, $scope){
      return{
        add: function(){

          'http://api.themoviedb.org/3/movie/206647?api_key=a8f7039633f2065942cd8a28d7cadad4&append_to_response=releases'
          // Search for release dates using the ID.
          var base = 'http://api.themoviedb.org/3/movie/';
          var movieID = $(event.currentTarget).parent().find('.movieID').text()
          var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
          var append_to_response = '&append_to_response=releases'
          var callback = 'JSON_CALLBACK'; // provided by angular.js
          var url = base + movieID + '?api_key=' + apiKey + append_to_response + '&callback=' + callback;

          $http.jsonp(url,{ cache: true}).
            success(function(data, status, headers, config) {

              if (status == 200) {

                $scope.movieListID = data;
                console.log($scope.movieListID);

                var releaseNL;

                for (var i = 0; i < $scope.movieListID.releases.countries.length; i++) {
                  var release = $scope.movieListID.releases.countries[i];
                  if (release['iso_3166_1'] == 'NL') {
                      releaseNL = release;
                  }
                }

                if(typeof releaseNL === 'undefined'){
                  // With release date

                  Notification($scope.movieListID.original_title + ' is toegevoegd, maar heeft nog geen Nederlandse premiere datum.');

                  createMovie.create({
                    title:          $scope.movieListID.original_title,
                    release_date:   $scope.movieListID.release_date,
                    image:          $scope.movieListID.poster_path,
                    movie_id:       $scope.movieListID.id
                  }).then(init);

                } else {
                  Notification.success($scope.movieListID.original_title + ' is toegevoegd.');

                  createMovie.create({
                    title:          $scope.movieListID.original_title,
                    release_date:   releaseNL.release_date,
                    image:          $scope.movieListID.poster_path,
                    movie_id:       $scope.movieListID.id
                  }).then(init);
                };

              } else {
                  console.error('Error happened while getting the movie list.')
              }
            })

            $(".search_results").fadeOut(250);

            $scope.searchquery = null
        }
      }
  }])

})();

在我的控制器中,我现在有,

$scope.addMovie = function() {

  addMovie.add();

};

现在当我触发addMovie函数时,我收到错误TypeError: Cannot read property 'jsonp' of undefined。有人能解释出错吗?

1 个答案:

答案 0 :(得分:1)

你没有正确地注射$http。您正在使用数组作为factory(...)中的第二个参数,但您没有给它注入字段的名称。

可能的解决方法:

angular.module('addMovieseat').factory('addMovie', ['$http', '$q', 
    function($http, $q) {...}]);

编辑:我删除了$scope,因为它无法注入工厂。