Javascript AngularJS为什么可以';我不能与其他模块共享一个提供程序吗?
我有一个应用程序模块,其中有许多组件模块被注入到应用程序模块中。应用程序模块有一个我想在组件模块内部使用的提供程序,但我收到一个注入器错误:Javascript AngularJS为什么可以';我不能与其他模块共享一个提供程序吗?,javascript,angularjs,angularjs-service,Javascript,Angularjs,Angularjs Service,我有一个应用程序模块,其中有许多组件模块被注入到应用程序模块中。应用程序模块有一个我想在组件模块内部使用的提供程序,但我收到一个注入器错误:error:$injector:modulerr module error 这就是我正在尝试的 var App = angular.module('App', [ 'ngRoute', 'Blog', 'Media', 'Pages', ]); App.provider('Core', function() { th
error:$injector:modulerr module error
这就是我正在尝试的
var App = angular.module('App', [
'ngRoute',
'Blog',
'Media',
'Pages',
]);
App.provider('Core', function() {
this.baseDirectory = '/test';
this.$get = function() {
var baseDirectory = this.baseDirectory;
}
});
App.config(['$routeProvider', 'CoreProvider', function($routeProvider, CoreProvider) {
$routeProvider
.when(CoreProvider.baseDirectory + '/admin', {
templateUrl: baseUrl + '/backend/scripts/angular/index.html'
})
.otherwise({
template: ""
});
}]);
当我尝试在另一个模块中使用CoreProvider
时,上面的所有代码都能正常工作,比如Blog(已经注入到App模块中)
我收到错误error:$injector:modulerr Module error
,并且Angular docs声明,当模块由于异常而无法加载时,会发生此错误
为什么我不能在我的博客模块中使用
CoreProvider
?如果您只需要共享一个基本目录,为什么不使用常量
var shared = angular.module('Shared',[]);
shared.constant('baseDirectory', '/test');
// Use the constant on your modules:
var module1 = angular.module('Module1', ['Shared']);
module1 .config(['baseDirectory', function(baseDirectory) {
console.log(baseDirectory + '/admin2');
}]);
var module2 = angular.module('Module2', ['Shared']);
module2.config(['baseDirectory', function(baseDirectory) {
console.log(baseDirectory + '/admina');
}]);
这是一个工作的PRUNKR:
我想这是一个更好的实践,有关更多信息,请参阅:在开发时使用AngularJS的未统一版本。它提供了更具可读性的错误消息SSO
glen
需要blog
和blog
需要glen
(虽然您在依赖项中没有提到)?很抱歉,Glenn
应该被App
替换为App
对于我的问题的演示,我在编辑中对其进行了更改
var shared = angular.module('Shared',[]);
shared.constant('baseDirectory', '/test');
// Use the constant on your modules:
var module1 = angular.module('Module1', ['Shared']);
module1 .config(['baseDirectory', function(baseDirectory) {
console.log(baseDirectory + '/admin2');
}]);
var module2 = angular.module('Module2', ['Shared']);
module2.config(['baseDirectory', function(baseDirectory) {
console.log(baseDirectory + '/admina');
}]);