想写一个全局的工具类,我在独立的js文件中声明ng service(并加载到html)
(function(angular){
"use strict";
angular.module('testModule',function(){
return {
test : function(){
console.info('testModule is ready!');
}
}
});
})(window.angular);
然后想在appmodule的controller里面注入testModule
,但提示注入失败
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: