3

I've been checking out AngularJS the last couple of days and I've run into an issue. I am trying to use $http.jsonp to get information from the Soundcloud API.... however it seems to me that $http is not defined in $scope. Here's what the console is telling me:

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

here is where I'm making the call in 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);
    }); 
  });
 };
});

Have I overlooked something? Should I not be tying to use $http.jsonp() in my $scope.init()? Does this mean that I am outside of $scope?

guizian
  • 67
  • 1
  • 1
  • 6

3 Answers3

12

You need to inject $http in the controller:

  .controller('MainCtrl', function ($scope, $http) {
AlwaysALearner
  • 43,759
  • 9
  • 96
  • 78
12

or

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

to prevent problems with code minification

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
0

I was getting error similar to this:

$location is undefined

I passed $location to the controller and it resolved the issue.

many thanks