Error: [$ injector: unpr] when injecting services into the controller

I know similar questions about this error have been asked many times on SO and I have read the answers, but as far as I can see my code is designed correctly. But of course it is not, because I am getting this error. My application has multiple services and multiple controllers, and for some reason they operate separately from one specific service / controller. I was able to keep the code to a minimum as shown below. The error appears when the TestController is run from the view using .. ng-controller="TestController" ..

, and the error message in the Chrome console is:

Error: [$ injector: unpr] http://errors.angularjs.org/1.2.15/ $ injector / unpr? p0 =% 24scopeProvider% 20% 3C-% 20% 24scope% 20% 3C-% 20Loyper in Error (native) ..

I have been struggling with this for some time and do not see that this service (Loyper) and controller (TestController) are defined differently than the ones that work in terms of injection. Any help would be appreciated.

var app = angular.module('testapp', [
"ngRoute",
"mobile-angular-ui"

]);

app.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$routeProvider.
when('/', {
    templateUrl: 'main.html',
    controller: 'TestController'
}).
when('/loyper', {
    templateUrl: 'loyper.html',
    controller: 'LoypeController'
}).
otherwise({
    redirectTo: '/'
});

$httpProvider.defaults.useXDomain = true;

}]);

app.service('Loyper', ['$scope', '$http', function($scope, $http) {

var cached = false;
var loyper = {};
var message;

return {
    foo: 'bar'
}

}]);

app.controller('TestController', ['$scope', 'Loyper', function($scope, Loyper) {

angular.extend($scope, {
    getLoyper: function() {
        return Loyper.foo;
    }

});

$scope.loypelist = $scope.getLoyper();

}]);

      

+3


source to share


1 answer


Service cannot be injected $scope

since the service is single point, whereas each controller and many directives have their own scope. So the problem is with this line:

app.service('Loyper', ['$scope', '$http', function($scope, $http) {

      

which should be



app.service('Loyper', ['$http', function($http) {

      

And in fact, since your service doesn't use $ http, it shouldn't accept the $ http argument either.

+5


source







All Articles