angularjsangular-ui-bootstrapnavbarangular-route-segment

Angular Routing and Bootstrap Navbar


I have problems getting the bootstrap navbar + angular routing to work. I don't understand what is going on since a very similar code on another app is working.

I have the following html in my index.html

<div ng-controller="CoreController as core">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">myApplication</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li ng-class="core.navHome"><a href="#home">Home <span class="sr-only">(current)</span></a></li>
                    <li ng-class="core.navStatistic"><a href="#statistic">Statistic</a></li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container-fluid -->
    </nav>
    <!-- angular templating -->
    <!-- this is where content will be injected -->
    <div ng-view></div>
</div>

my app.js:

            $routeProvider.when('/', { templateUrl: 'templates/home.html', controller: 'MainController', controllerAs: 'main' });
            $routeProvider.when('/statistic', { templateUrl: 'templates/statistic.html', controller: 'StatisticController', controllerAs: 'statistic' });
           // Default route
             $routeProvider.otherwise({ redirectTo: '/' });

When I hit the statistic link, my URL shows: http://localhost/myApplication/#!/#statistic and I get see the home template.

The home button then shows "http://localhost/myApplication/#!/"

Is the #! the reason for this?


Solution

  • Ok after some research I found out that I this code worked for angular < 1.6.

    To make the above code work in 1.6 you have to get ridd of the #! by adding this to the config:

    $locationProvider.hashPrefix('');
    

    Here is the original Post.