Michelle Colin
Michelle Colin

Reputation: 79

Angular JS adding a service to controller is not working

I had this:

.controller('dashboardCtrl', function($scope, $timeout, $location, bionicoService, bionicoSamples, $state, $locale){

And everything worked fine, i could call my services for example bionicoSamples without a problem But i had to add a module for i18n and it requires this

.controller('dashboardCtrl', ['jlgI18nService', function( i18nService){

So when i added it to my code, my services stoped working, and i have no idea why, i'm king of new in angular and there is some conceptual stuff i don't understand well yet, for instance i really don't understand the difference betweem using "['module', function()" to "function('module')"

My code after trying to add the module is lilke this:

.controller('dashboardCtrl', ['jlgI18nService', function($scope, $timeout, $location, bionicoService, bionicoSamples, $state, $locale, i18nService){

I've also tried this, but is not working too

.controller('dashboardCtrl', ['jlgI18nService', '$scope', '$timeout', '$location', 'bionicoService',  '$state', '$locale', function($scope, $timeout, $location, bionicoService, bionicoSamples, $state, $locale, i18nService){

The error i'm getting is that my services are undefined, if i try to call bionicoSamples.function, it says that this ""function"" is not a function. I know the problem is in the way i'm importing the services and modules now with this i18n module, because before it was working fine

Thanks!

Upvotes: 0

Views: 75

Answers (1)

logee
logee

Reputation: 5067

From the angular docs when using 'Inline Array Annotation' to declare your controller

When using this type of annotation, take care to keep the annotation array in sync with the parameters in the function declaration.

The order that you're injecting your services is wrong and you have more inputs to your function than injected services. Should be something like this:

.controller('dashboardCtrl', [
    '$scope',
    '$timeout',
    '$location',
    'bionicoService', 
    'bionicoSamples', // This is missing in yours
    '$state',
    '$locale',
    'jlgI18nService',
    function($scope, $timeout, $location, bionicoService, bionicoSamples, $state, $locale, i18nService){

Upvotes: 1

Related Questions