【问题标题】:Error: [$injector:unpr] Unknown provider: $stateProvider错误:[$injector:unpr] 未知提供者:$stateProvider
【发布时间】:2015-08-31 07:42:28
【问题描述】:

我在尝试使用 ui.router 时遇到错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module bApp 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:9000/bower_components/angular/angular.js:68:12)
at http://localhost:9000/bower_components/angular/angular.js:4284:19
at getService (http://localhost:9000/bower_components/angular/angular.js:4432:39)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4464:13)
at runInvokeQueue (http://localhost:9000/bower_components/angular/angular.js:4379:35)
at http://localhost:9000/bower_components/angular/angular.js:4388:11
at forEach (http://localhost:9000/bower_components/angular/angular.js:336:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4369:5)
at createInjector (http://localhost:9000/bower_components/angular/angular.js:4294:11)
at doBootstrap (http://localhost:9000/bower_components/angular/angular.js:1655:20)
http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=bApp&p1=Error%3A%20…F%2Flocalhost%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A20)

angular.module('bApp', ['ui.router']);
angular.module('bApp').config(function($stateProvider, $urlRouterProvider) {
    urlRouterProvider.otherwise('/');

    $stateProvider
        .state('main', {
            url: "/",
            templateUrl: "/views/main.html"
        })

    .state('register', {
        url: "/register",
        templateUrl: "/views/register.html"
       
    });
});
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <!-- bower:css -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <!-- endbower -->
    <!-- endbuild -->
    <!-- build:css(.tmp) styles/main.css -->
    <link rel="stylesheet" href="styles/bootstrap-hero.css">
    <link rel="stylesheet" href="styles/main.css">
    <!-- endbuild -->
    <!-- build:js(.) scripts/vendor.js -->
  
    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>

    <!-- endbuild -->


</head>

<body ng-app="bApp">

    <!--[if lte IE 8]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <div class="header">
        <div class="navbar navbar-default" role="navigation">
            <div class="container">
                <div class="navbar-header">

                    <a class="navbar-brand" href="#/">b</a>
                </div>
                <ul class="nav navbar-nav">
                    <li><a ui-sref="main">Home</a></li>
                    <li><a ui-sref="register">Register</a></li>
                </ul>
            </div>
        </div>
    </div>


    <div ui-view></div>


    <!-- build:js({.tmp,app}) scripts/scripts.js -->
  
    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/controllers/register.js"></script>
    <script src="scripts/app.config.js"></script>
    <!-- endbuild -->
    </div>
</body>
</html>

我正在尝试使用 ui.router 但它失败了。

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    由于您的 Angular 脚本标签位于 html 头部,因此在使用时并非所有模块都已完成加载的可能性很小。

    根据Where should I put <script> tags in HTML markup?,这里有两种选择:

    1.在头部的脚本标签中添加defer,这可能不适用于所有浏览器:

    <script src="bower_components/jquery/dist/jquery.js" defer></script>
    <script src="bower_components/angular/angular.js" defer></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js" defer></script>
    <script src="bower_components/angular-ui-router/release/angular-ui-router.js" defer></script>
    

    2. 将它们放在身体底部。这适用于所有浏览器。

        ...
    
        <script src="bower_components/jquery/dist/jquery.js" defer></script>
        <script src="bower_components/angular/angular.js" defer></script>
        <script src="bower_components/bootstrap/dist/js/bootstrap.js" defer></script>
        <script src="bower_components/angular-ui-router/release/angular-ui-router.js" defer></script>
    
        <!-- build:js({.tmp,app}) scripts/scripts.js -->
    
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/main.js"></script>
        <script src="scripts/controllers/register.js"></script>
        <script src="scripts/app.config.js"></script>
        <!-- endbuild -->
        </div>
    </body>
    

    【讨论】:

    • 谢谢,但我先尝试了,但没有成功 :(,然后我将它们添加到头部。
    • defer 是做什么的?
    • @James:在页面完成加载之前,不会执行带有defer 属性的外部脚本。另请参阅The HTML Script Element
    【解决方案2】:

    尝试在没有ui的情况下安装角度路由器

    https://github.com/angular/bower-angular-route

    然后不要忘记将其包含在项目中。

    <script src="/js/bower_components/angular-route/angular-route.js"></script>
    

    【讨论】:

    • 切换到不同的库并不能解决原来的问题。
    • 不过,这可能是一个务实的解决方案。
    • 可以,但在这种情况下,至少应该说明为什么应该交换库。这是一个重大变化,我认为很难找到反对使用 ui-router 的论据。
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2016-03-26
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多