角度错误:错误:[$ injector:unpr]未知提供者:$ stateProvider

时间:2015-09-05 20:27:54

标签: angularjs ngroute

我已经阅读了有关该主题的其他相关主题,并尝试了他们推荐的解决方案,但我无法弄明白。据我所知,ngRoute通过标签正确链接到项目,然后作为依赖项注入。我尝试了许多不同的东西(比如使用ui-router),但似乎没有任何工作。任何帮助将不胜感激!

完整错误如下:

    Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:unpr] Unknown provider: $stateProvider
http://errors.angularjs.org/1.4.5/$injector/unpr?p0=%24stateProvider
    at REGEX_STRING_REGEXP (http://localhost:8000/static/bower_components/angular/angular.js:68:12)
    at http://localhost:8000/static/bower_components/angular/angular.js:4284:19
    at getService (http://localhost:8000/static/bower_components/angular/angular.js:4432:39)
    at Object.invoke (http://localhost:8000/static/bower_components/angular/angular.js:4464:13)
    at runInvokeQueue (http://localhost:8000/static/bower_components/angular/angular.js:4379:35)
    at http://localhost:8000/static/bower_components/angular/angular.js:4388:11
    at forEach (http://localhost:8000/static/bower_components/angular/angular.js:336:20)
    at loadModules (http://localhost:8000/static/bower_components/angular/angular.js:4369:5)
    at createInjector (http://localhost:8000/static/bower_components/angular/angular.js:4294:11)
    at doBootstrap (http://localhost:8000/static/bower_components/angular/angular.js:1655:20)
http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=myApp&p1=Error%3A%2…host%3A8000%2Fstatic%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A20)

的index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body ng-app="myApp">

    <div ng-controller="MainCtrl">
        {{ test }}
    </div>

    <script src="static/bower_components/angular/angular.js"></script>
    <script src="static/bower_components/angular-route/angular-route.min.js"></script>
    <script src="static/components/app.js"></script>
</body>
</html>

app.js:

'use strict';

function() {
    angular.module('myApp', ['ngRoute'])
        .controller('MainCtrl', function ($scope) {
            $scope.test = 'hello world';
        });
})();

3 个答案:

答案 0 :(得分:1)

这不是路由问题。您的app.js中缺少括号,您的代码应该是这样的。

'use strict';

(function () {
    angular.module('myApp', ['ngRoute'])
        .controller('MainCtrl', function ($scope) {
            $scope.test = 'hello world';
        });
})();

答案 1 :(得分:0)

我认为ngRoute(angular-route.js)使用提供者$ routeProvider,而ui-router(angular-ui-router.js)使用提供者$ stateProvider。

看看这个答案。 What is the difference between angular-route and angular-ui-router?

如果是这种情况。 解决方案1.将脚本从angular-route.js更改为angular-ui-router.js 解决方案2.将提供程序更改为$ routeProvider

希望有所帮助

答案 2 :(得分:0)

如果以上所有答案都失败了,那就试试吧。

我认为问题在于附带的脚本......

解决方案1:将angular.min.js和angular-route.min.js复制到当前文件夹并将其包含为

<script src="angular.min.js"></script>

如果不起作用,

解决方案2:                 尝试在标题标记结束后的头标记上添加上述脚本。