未知提供者:$state
Unknown provider: $state
您好,我在通过 $injector 服务进行依赖项注入时遇到了一些问题。
我是 angular 的新手,所以我将从 IoC 容器的角度解释我是如何看待这一点的。
首先是问题:$injector 无法解析 $state。
core.js
(function() {
'use strict';
angular.module('app.core',['ui.router'])
.config(function($stateProvider,$state){
// ......
});
}());
这是错误:
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.core due to:
Error: [$injector:unpr] Unknown provider: $state
我认为堆栈跟踪在这里没有多大意义......但以防万一我将其发布在问题的底部:
Index.html :我只是想展示我引用 .js 文件的位置和顺序。
<head>
.... <!-- pointing out that my .js are not here -->
</head>
<body>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script src="components/core/lib/core.js"></script>
**<!-- relevant to the EDIT part below -->
<script src="components/something/something.js"></script>**
</body>
据我了解 angular 的提供商配方:
$stateProvider is the service provider which registers $state service to the IoC($injector) and does so by exposing $get member which the IoC knows to treat in a special way.
如果创建了“$stateProvider”并且可以自行注入它为什么没有向 IoC 注册“$state”服务
EDIT :此外,我稍后还有另一个模块可以毫无问题地接收 $state 服务作为依赖项。
something.js
(function() {
'use strict';
angular.module('app.something',['ui.router'])
.config(function($stateProvider, $urlRouterProvider,$state) {
.... no problem receiving '$state' here
})
}());
编辑 2:不好意思它在任何配置中都不起作用 VISHAL DAGA 是正确的。
堆栈跟踪:
http://errors.angularjs.org/1.4.8/$injector/unpr?p0=%24state
at http://localhost:9000/bower_components/angular/angular.js:68:12
at http://localhost:9000/bower_components/angular/angular.js:4334:19
at getService (http://localhost:9000/bower_components/angular/angular.js:4482:39)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4514:13)
at runInvokeQueue (http://localhost:9000/bower_components/angular/angular.js:4429:35)
at http://localhost:9000/bower_components/angular/angular.js:4438:11
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4419:5)
at http://localhost:9000/bower_components/angular/angular.js:4436:40
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=app.core&p1=Error%3… 2F%2Flocalhost%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A340%3A20)
at http://localhost:9000/bower_components/angular/angular.js:68:12
at http://localhost:9000/bower_components/angular/angular.js:4458:15
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4419:5)
at http://localhost:9000/bower_components/angular/angular.js:4436:40
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4419:5)
at createInjector (http://localhost:9000/bower_components/angular/angular.js:4344:11)
at doBootstrap (http://localhost:9000/bower_components/angular/angular.js:1676:20)
at bootstrap (http://localhost:9000/bower_components/angular/angular.js:1697:12)
http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=app&p1=Error%3A%20%…F%2Flocalhost%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A1697%3A12)
在配置阶段,它都是提供者,所以注入 $stateProvider 是正确的,但不是 $state。
删除 $state,问题就会消失。
我认为您将 $state
与 $stateProvider.state
混为一谈。正确使用见下文
(function() {
'use strict';
angular.module('app.core',['ui.router'])
.config(function($stateProvider){
$stateProvider
.state('default', {....
});
}());
您好,我在通过 $injector 服务进行依赖项注入时遇到了一些问题。
我是 angular 的新手,所以我将从 IoC 容器的角度解释我是如何看待这一点的。
首先是问题:$injector 无法解析 $state。
core.js
(function() {
'use strict';
angular.module('app.core',['ui.router'])
.config(function($stateProvider,$state){
// ......
});
}());
这是错误:
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.core due to:
Error: [$injector:unpr] Unknown provider: $state
我认为堆栈跟踪在这里没有多大意义......但以防万一我将其发布在问题的底部:
Index.html :我只是想展示我引用 .js 文件的位置和顺序。
<head>
.... <!-- pointing out that my .js are not here -->
</head>
<body>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script src="components/core/lib/core.js"></script>
**<!-- relevant to the EDIT part below -->
<script src="components/something/something.js"></script>**
</body>
据我了解 angular 的提供商配方:
$stateProvider is the service provider which registers $state service to the IoC($injector) and does so by exposing $get member which the IoC knows to treat in a special way.
如果创建了“$stateProvider”并且可以自行注入它为什么没有向 IoC 注册“$state”服务
EDIT :此外,我稍后还有另一个模块可以毫无问题地接收 $state 服务作为依赖项。
something.js
(function() {
'use strict';
angular.module('app.something',['ui.router'])
.config(function($stateProvider, $urlRouterProvider,$state) {
.... no problem receiving '$state' here
})
}());
编辑 2:不好意思它在任何配置中都不起作用 VISHAL DAGA 是正确的。
堆栈跟踪:
http://errors.angularjs.org/1.4.8/$injector/unpr?p0=%24state
at http://localhost:9000/bower_components/angular/angular.js:68:12
at http://localhost:9000/bower_components/angular/angular.js:4334:19
at getService (http://localhost:9000/bower_components/angular/angular.js:4482:39)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4514:13)
at runInvokeQueue (http://localhost:9000/bower_components/angular/angular.js:4429:35)
at http://localhost:9000/bower_components/angular/angular.js:4438:11
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4419:5)
at http://localhost:9000/bower_components/angular/angular.js:4436:40
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=app.core&p1=Error%3… 2F%2Flocalhost%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A340%3A20)
at http://localhost:9000/bower_components/angular/angular.js:68:12
at http://localhost:9000/bower_components/angular/angular.js:4458:15
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4419:5)
at http://localhost:9000/bower_components/angular/angular.js:4436:40
at forEach (http://localhost:9000/bower_components/angular/angular.js:340:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4419:5)
at createInjector (http://localhost:9000/bower_components/angular/angular.js:4344:11)
at doBootstrap (http://localhost:9000/bower_components/angular/angular.js:1676:20)
at bootstrap (http://localhost:9000/bower_components/angular/angular.js:1697:12)
http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=app&p1=Error%3A%20%…F%2Flocalhost%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A1697%3A12)
在配置阶段,它都是提供者,所以注入 $stateProvider 是正确的,但不是 $state。
删除 $state,问题就会消失。
我认为您将 $state
与 $stateProvider.state
混为一谈。正确使用见下文
(function() {
'use strict';
angular.module('app.core',['ui.router'])
.config(function($stateProvider){
$stateProvider
.state('default', {....
});
}());