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?

3 Answers 3

12

You need to inject $http in the controller:

  .controller('MainCtrl', function ($scope, $http) {
Sign up to request clarification or add additional context in comments.

Comments

12

or

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

to prevent problems with code minification

Comments

0

I was getting error similar to this:

$location is undefined

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

many thanks

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.