Uthistran Selvaraj
Uthistran Selvaraj

Reputation: 1371

angular service injection is not consistent

I am beginner in AngularJS

I have an angular service as below

angular.module('TestModule', []).service('testservice', ['$timeout', testservice]);
function testservice($timeout) {
    /* my function */
}

I am injecting this module in my main module as below in app.js

angular.module("MainModule", ['TestModule']);

and trying to inject the testservice in my controller as below

(function () {
'use strict';
angular
.module('MainModule')
.controller('testController', ['$scope', '$state', 'testservice',  testController]); 
function testController($scope, $state, testservice ) {
/*I have my functions here */
}

})();

Sometimes, it gets injected successfully and in sometimes it throws error as below

Unknown provider: testserviceProvider <- testservice <- testController

I cannot guess out the problem and I stuck completely with it. What I am doing wrong?

Upvotes: 1

Views: 74

Answers (1)

Estus Flask
Estus Flask

Reputation: 222760

The code above can't cause this error

Unknown provider: testserviceProvider <- testservice <- testController

This can happen if TestModule that contains testservice becomes accidentally overriden with another definition of TestModule:

angular.module('TestModule', []);

If the error appears and disappears randomly, this means that in some cases file that contains TestModule module with testservice is loaded before file that contains overridden TestModule, and in some cases it's not.

Upvotes: 1

Related Questions