Question

Im having problem with injections.

app.js

angular.module('Help', []);
var app = angular.module('app', [
    'restangular'
    ,'Help'
]); 
app.$inject = ['RestangularProvider']; 
app.config(
    function(RestangularProvider) {
        RestangularProvider.setBaseUrl('http://localhost:8080/api');
    }
)

help.js

function HelpCtrl($rootScope, $scope, Restangular){
 Restangular.one('questions').getList();
}
HelpCtrl.$inject = ['$scope', '$rootScope','Restangular'];  //"TAG1"
angular.module('Help').controller("HelpCtrl", HelpCtrl); 

I get the following error:

Uncaught TypeError: Object [object Object] has no method 'one' 

If I remove line TAG1, everything works. However I need to inject it the right way. What is the problem here?

Was it helpful?

Solution

The injector is used to help AngularJS know the order of parameters given to the function in case the variable name change (e.g. after minimizing your JavaScript).

In your case, you've switched up the order of your injector parameters and the method signature, meaning that AngularJS will think that $scope is $rootScope and vice versa.

Either remove your $inject or make sure the parameters are in the same order in both your method signature and in your injection array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top