1

I'm trying to make an HTTP request to the Last.fm API using Angular.js but I can't get it to work. I have separated out my Angular js files and have compiled them into one single js file called scripts.js using Codekit. The order in which the files are compiled is:

  • angular.min.js
  • app.js
  • controllers.js
  • services.js

Here is what my files look like:

app.js

var app = angular.module('app', []);

controllers.js

app.controller('similarArtistsController', function($scope, similarArtistsService) {

    $scope.artists = [];

    similarArtistsService.getArtists().success(function(response) {

        console.log(response);
    });

});

services.js

app.factory('similarArtistsService', function($http) {

    var similarArtists = {};

    similarArtists.getArtists = function() {

        return $http({
            method: 'GET', 
            url: 'http://ws.audioscrobbler.com/2.0/?method=artist.getSimilar&api_key=MYLASTFMAPIKEY&format=json&limit=5&artist=Tame+Impala'
        });
    }

    return similarArtists;
});

index.html

<body>

    <div ng-app="app">

        <div ng-controller="similarArtistsController"></div>

    </div>

    <script src="/js/compiled/scripts.js"></script>

</body>

In my console I see "Error: [$injector:unpr]" which I've learned to mean that the controller cannot resolve a dependency. In my case I believe it has something to with my service I'm injecting, but I don't know where the error lies.

4 Answers 4

2

Does compiling the scripts minify them too? If so, you need to declare your dependencies in an array...

app.controller('similarArtistsController', ['$scope', 'similarArtistsService', function($scope, similarArtistsService) {
    $scope.artists = [];
    similarArtistsService.getArtists().success(function(response) {
        console.log(response);
    });
}]);

app.factory('similarArtistsService', ['$http', function($http) {
    var similarArtists = {};
    similarArtists.getArtists = function() {
        return $http({
            method: 'GET', 
            url: 'http://ws.audioscrobbler.com/2.0/?method=artist.getSimilar&api_key=MYLASTFMAPIKEY&format=json&limit=5'
        });
    }
    return similarArtists;
}]);

Without that, Angular uses parameter names to resolve dependencies. Many minifiers mangle those names.

Sign up to request clarification or add additional context in comments.

4 Comments

Codekit does minify the scripts as well. I've added the dependencies for both the controller and service ($http) but it still isn't working. Now I am getting "Error: [ng:areq]."
Seems to be calling the service (and getting invalid api key error) when I try it. Fiddle... jsfiddle.net/K6GKA
Didn't see your updated question. The function needs to be in the array.
This does work. When I first added the dependencies, I closed the array too soon. Moving the closing bracket to the end of the controller and service worked.
0

Include your services before your controllers so the service is known at time of injection

1 Comment

worked for me when I had this bug a while back. Depends how you structure your application. My newest applications list all of the controllers, services, and filters in one object, and then they get binded separately. Making use of $provide, $compileProvider, $controllerProvider, $filterProvider can allow you to do this. example Object.each(hash['services'], function(k, v) { $provide.factory(k, v); });
0

Can you try switching to app.service('similarArtistsService', function($http) {}); since this will return the function instance as opposed to factory which will return the value.

Comments

0

When you declare your dependencies in AngularJS, don't close off the array until after the function. See square brackets of array:

controllers.js

app.controller('similarArtistsController', ['$scope', 'similarArtistsService', function($scope, similarArtistsService) {
  // code in here
}]);

services.js

app.factory('similarArtistsService', ['$http', function($http) {
  // code in here
}]);

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.