Angular JS$http未在作用域中定义



过去几天我一直在查看AngularJS,遇到了一个问题。我正在尝试使用$http.jsonp从Soundcloud API获取信息。。。。然而,在我看来,$http并没有在$scope中定义。以下是控制台告诉我的内容:

Uncaught ReferenceError: $http is not defined main.js:18
(anonymous function) main.js:18
(anonymous function) sdk.js:1
window.SC.SC.Helper.merge.Dialog.AbstractDialog.AbstractDialog.handleReturn sdk.js:1
window.SC.SC.Helper.merge.Dialog._handleDialogReturn sdk.js:1
window.SC.SC.Helper.merge.connectCallback

这是我用main.js 打电话的地方

angular.module('soundSelectahApp')
  .controller('MainCtrl', function ($scope) {
    $scope.apiKey = "#############################";
    $scope.results = [];
    $scope.init = function(){
        SC.initialize({
        client_id: $scope.apiKey,
        redirect_uri: "http://localhost:9000/callback.html"
        });
    // initiate auth popup
    SC.connect(function() {
        SC.get('/me', function(me) { 
        alert('Hello, ' + me.username); 
    });
    $http.jsonp('https://api.soundcloud.com/me.json?client_id=' + $scope.apiKey + '&callback=JSON_CALLBACK').success(function(data) {
        console.log(data);
    }).error(function(error) {
        console.log(error);
    }); 
  });
 };
});

我是不是忽略了什么?我不应该在$scope.init()中绑定使用$http.jsonp()吗?这是否意味着我在$scope之外?

您需要在控制器中注入$http:

  .controller('MainCtrl', function ($scope, $http) {

.controller('MainCtrl',['$scope','$http',函数($scope,$http){

以防止代码缩小出现问题

我得到了类似的错误:

$location未定义

我将$location传递给控制器,它解决了问题。

非常感谢

最新更新