0

I'm building a web app with angular for the first time and have use the bootstraps css (angular ui). I'm using the navbar from bootstrap and route to ng-templates with ng-route. Everything works fine.

Now I wanted to include a dropdown button into the navbar where I can reach some additional links for routing. I made it up like so:

<nav class="navbar navbar-default navbar-static-top" role="navigation" ng-controller="NavigationCtrl">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" ng-click="nav.collapsed=!nav.collapsed">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <div class="navbar-collapse navbar-right" collapse="nav.collapsed">
            <ul class="nav navbar-nav">
                <li>
                    <a href="#/index" ng-click="nav.collapsed=true">
                        <span class="glyphicon glyphicon-home"></span>
                        Home
                    </a>
                </li>
                <li class="dropdown">
                    <a href="" class="dropdown-toggle" data-toggle="dropdown">
                        <span class="glyphicon glyphicon-cog"></span>
                        Dropdown
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu dropdown-toggle" data-toggle="dropdown" role="menu">
                        <li>
                            <a href="#/configA" ng-click="nav.collapsed=true">
                                <span class="glyphicon glyphicon-dashboard"></span>
                                ConfigA
                            </a>
                        </li>
                        <li>
                            <a href="#/configB" ng-click="nav.collapsed=true">
                                <span class="glyphicon glyphicon-dashboard"></span>
                                ConfigB
                            </a>
                        </li>
                        <li>
                            <a href="#/configC" ng-click="nav.collapsed=true">
                                <span class="glyphicon glyphicon-dashboard"></span>
                                ConfigC
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </nav>

My route looks like this:

app.config(["$routeProvider", function ($routeProvider) {
    $routeProvider
        // route for the main page
        .when("/index", {
            templateUrl: "/index"
        })
        // route for the about page
        .when("/configA", {
            templateUrl: "/configA"
        })
        // route for the about page
        .when("/configB", {
            templateUrl: "/configB"
        })
        // route for the about page
        .when("/configC", {
            templateUrl: "/configC"
        })
        .otherwise({redirectTo: "/index"});
}])

My templates are in the html head as scripts like:

<script type="text/ng-template" id="/index">
    <div>
        <div class="jumbotron">
            <h1>Hello, world!</h1>
            <p>...</p>
            <p><a class="btn btn-primary btn-lg" role="button">Learn more</a></p>
        </div>
    </div>
</script>

This works fine for usual links in the navbar. But clicking on the links in the dropdown list wont work, just nothing happens :( Have I missed something here?

Fidel90
  • 1,828
  • 6
  • 27
  • 63

2 Answers2

0

You to have to create an ancor link.

Just create a real link and it should work fine :

<nav class="navbar navbar-default navbar-static-top" role="navigation" ng-controller="NavigationCtrl">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" ng-click="nav.collapsed=!nav.collapsed">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
    </div>
    <div class="navbar-collapse navbar-right" collapse="nav.collapsed">
        <ul class="nav navbar-nav">
            <li>
                <a href="/index" ng-click="nav.collapsed=true">
                    <span class="glyphicon glyphicon-home"></span>
                    Home
                </a>
            </li>
            <li class="dropdown">
                <a href="" class="dropdown-toggle" data-toggle="dropdown">
                    <span class="glyphicon glyphicon-cog"></span>
                    Dropdown
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu dropdown-toggle" data-toggle="dropdown" role="menu">
                    <li>
                        <a href="/configA" ng-click="nav.collapsed=true">
                            <span class="glyphicon glyphicon-dashboard"></span>
                            ConfigA
                        </a>
                    </li>
                    <li>
                        <a href="/configB" ng-click="nav.collapsed=true">
                            <span class="glyphicon glyphicon-dashboard"></span>
                            ConfigB
                        </a>
                    </li>
                    <li>
                        <a href="/configC" ng-click="nav.collapsed=true">
                            <span class="glyphicon glyphicon-dashboard"></span>
                            ConfigC
                        </a>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
</nav>
  • That doesn't work neither. I have to use # as I'm working with local files and so I'm only able to work with ng-templates that just can be referred to by #/addresses. As I said this works as it should with all the standard navbar links but not with the one from the dropdown.. – Fidel90 Oct 01 '14 at 09:53
  • Does it still happen when you remove the ng-click="nav.collapsed=true" ? – Christophe Dufour Oct 01 '14 at 09:57
  • Yes, no change after removing it :( Following the link manually works btw, so I guess there's no problem with the route config. – Fidel90 Oct 01 '14 at 09:59
0

It seems like I have to remove "dropdown-toggle" from the dropdowns ul classlist as well as the data-toggle="dropdown" attribute. Now it works like a charm :)

Solved thanks to this answer: https://stackoverflow.com/a/18546378/2577116

Community
  • 1
  • 1
Fidel90
  • 1,828
  • 6
  • 27
  • 63