且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不能调用未定义的方法'JSONP“在Angular.js控制器

更新时间:2023-02-12 13:22:33

我猜你没有正确注入依赖

  app.controller(小于CONTROLLER_NAME>中['$范围',函数($范围内){}]);

在你的情况应该是

  app.controller('ImagesCtrl',['$范围,$ HTTP',函数($范围,$ HTTP){}]);

或者,如果你preFER不使用注释(这有利于缩小):

  app.controller('ImagesCtrl',函数(){
    的console.log(在控制器);
});

The following controller is producing the error message "Cannot call method 'jsonp' of undefined". I suspect that I am not injecting $http properly. Can someone tell me what I've done incorrectly?

'use strict';

/* Controllers */

angular.module('myApp.controllers', []).

  controller('ImagesCtrl', [function ImagesCtrl ($scope, $http) {
    $http.jsonp('http://localhost:3000/image?quantity=1&language=Cantonese&callback=JSON_CALLBACK')
    .success(function(data){
      console.log(data);
      $scope.image = data;
    }); 

  }])


  .controller('CaptionsCtrl', [function() {

  }]);

I guess you are not injecting dependencies properly

app.controller(<controller_name>, ['$scope', function($scope) {}]);

in your case that should be

app.controller('ImagesCtrl', ['$scope', '$http', function($scope, $http) {}]);

Or if you prefer not to use annotations (which are good for minification):

app.controller('ImagesCtrl', function () {
    console.log("in the controller");
});