I want to write a global tool class, I declare ng service in a separate js file (and load it into html)
(function(angular){
"use strict";
angular.module('testModule',function(){
return {
test : function(){
console.info('testModule is ready!');
}
}
});
})(window.angular);
Then I want to inject testModule
into the controller of appmodule, but it prompts that the injection failed
var mainApp = angular.module('mainApp',[]);
mainApp.controller('homeController',['testModule',function(testModule){
testModule.test();
}]);
"Error: [$injector:unpr] Unknown provider: testModuleProvider
Thank you, what you wrote is only 30% correct. Two topics are involved here:
1. Different modules
angular.module('testModule')
和angular.module('mainApp',[])
是两个不同的module,你想要在mainApp
中使用testModule
,那么你需要在angular.module('mainApp', [ 'testModule' ])
Import this dependency.2. The global tool class is written incorrectly
Someone answered it above:
When in use, you can:
The above code has not been verified and there should be no structural problems. Please debug and solve it by yourself.
Just define your service as a module, for example: