6

I'm getting an error trying to use 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>

I'm trying to use ui.router but it is failing.

frhd
  • 9,396
  • 5
  • 24
  • 41
becki
  • 131
  • 1
  • 6

2 Answers2

7

Because your angular script tags are in the html head, there is slight chance that not all modules are done loading when they are being used.

According to Where should I put <script> tags in HTML markup?, you have two options here:

1. Add a defer to your script tags in the head, which might not work for all browsers:

<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. Put them at the bottom of your body. This will work for all browsers.

    ...

    <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>
Community
  • 1
  • 1
frhd
  • 9,396
  • 5
  • 24
  • 41
  • Thank you, But I tried that first and didn't work :(, then I added them to the head. – becki Aug 31 '15 at 17:50
  • What does *defer* do? – James Vickery Aug 24 '16 at 16:14
  • 2
    @James: An external script attributed with `defer` will not be executed until the page has finished loading. See also [The HTML Script Element](https://developer.mozilla.org/de/docs/Web/HTML/Element/script). – frhd Aug 25 '16 at 11:41
0

try to install angular router without ui

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

don't forget include it in project then.

<script src="/js/bower_components/angular-route/angular-route.js"></script>
Ignat Galkin
  • 597
  • 1
  • 4
  • 11
  • 7
    Switching to a different library is not the answer to the original problem. – frhd Aug 31 '15 at 07:54
  • It can be a pragmatic solution, though. – Risadinha Aug 31 '15 at 08:36
  • 1
    It can be, but in this case, at least a reason should be given as to _why_ the library should be swapped. This is a major change, and I assume it's hard to find arguments against using `ui-router`. – frhd Aug 31 '15 at 08:41