我是AngularJS的初学者

我有如下的角度服务

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


我在app.js中将这个模块注入到我的主模块中

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


并尝试将testservice注入到我的控制器中,如下所示

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

})();


有时,它被成功注入,有时它会抛出如下错误


  未知的提供程序:testserviceProvider

我无法猜出问题,我完全坚持了下来。我做错了什么?

最佳答案

上面的代码不会导致此错误


  未知的提供程序:testserviceProvider

如果包含TestModuletestservice被另一个TestModule定义意外覆盖,则会发生这种情况:

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


如果错误出现并随机消失,则意味着在某些情况下,包含TestModule模块和testservice的文件将在包含被覆盖的TestModule的文件之前加载,而在某些情况下则不会。

09-25 15:54