Twitter Bootstrap Navbar with AngularJS - Collapse Not Functioning

后端 未结 8 1053
夕颜
夕颜 2020-12-02 05:33

I am using Angular and Twitter Bootstrap navbar and trying to get the collapse functionality to work.

Partial: program.html

相关标签:
8条回答
  • 2020-12-02 05:57

    For those interested - Here is another way of implementing this without Bootstrap's javascript.

    Import Angular's UI-Bootstrap.

    HTML:

    <div class="navbar navbar-inverse" ng-controller="NavBarCtrl">
    <div class="navbar-inner">
        <div class="container">
            <button class="btn btn-navbar" ng-click="isCollapsed = !isCollapsed"> 
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
            </button> <a class="brand" href="#">Short Course</a>
            <div class="nav-collapse" uib-collapse="isCollapsed">
                <ul class="nav">
                    <li><a href="#"><i class="icon-home icon-white"></i> Home</a>
                    </li>
                    <li><a href="#">Lessons</a>
                    </li>
                    <li><a href="#">Grades</a>
                    </li>
                </ul>
                <ul class="nav pull-right">
                    <li><a href="#/class"><i class="icon-upload icon-white"></i> Upload/Save</a>
                    </li>
                    <li><a href="#/class"><i class="icon-off icon-white"></i> Save/Logout</a>
                    </li>
                </ul>
            </div>
            <!-- /.nav-collapse -->
        </div>
    </div>
    <!-- /navbar-inner -->
    </div>
    

    JS:

    var myApp = angular.module('myApp', ['ui.bootstrap']);
    
    function NavBarCtrl($scope) {
        $scope.isCollapsed = true;
    }
    

    And the fiddle - http://jsfiddle.net/KY5Mf/

    0 讨论(0)
  • 2020-12-02 05:59

    This was a tricky one. The docs showed one way, and it functions great. I copied the docs example (http://twitter.github.com/bootstrap/components.html#navbar) and tried using it. I then went to the examples page and tried the layout listed here: http://twitter.github.com/bootstrap/examples/fluid.html

    The one and only difference was a <button> instead of <a>

    <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
    

    Instead of

    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
    

    I don't know why, but changing that made it function great.

    EDIT

    Arbiter pointed out that the <a /> was missing the href='#' attribute. Adding that attribute would also solve the problem.

    0 讨论(0)
  • 2020-12-02 06:00

    No need to engage a controller. Just use ng-init instead to initialize the isCollapsed flag when template is initially compiled.

    <div class="navbar navbar-inverse" ng-controller="NavBarCtrl">
    <div class="navbar-inner">
        <div class="container">
            <button class="btn btn-navbar" ng-init="isCollapsed = true" ng-click="isCollapsed = !isCollapsed"> 
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
            </button> <a class="brand" href="#">Short Course</a>
            <div class="nav-collapse" collapse="isCollapsed">
                <ul class="nav">
                    <li><a href="#"><i class="icon-home icon-white"></i> Home</a>
                    </li>
                    <li><a href="#">Lessons</a>
                    </li>
                    <li><a href="#">Grades</a>
                    </li>
                </ul>
                <ul class="nav pull-right">
                    <li><a href="#/class"><i class="icon-upload icon-white"></i> Upload/Save</a>
                    </li>
                    <li><a href="#/class"><i class="icon-off icon-white"></i> Save/Logout</a>
                    </li>
                </ul>
            </div>
            <!-- /.nav-collapse -->
        </div>
    </div>
    <!-- /navbar-inner -->
    </div>
    
    0 讨论(0)
  • 2020-12-02 06:04

    Just adding jquery.min.js and bootstrap.min.js to the index.html file solved this problem.

    For collapsable navigation we need to include jquery.min.js and bootstrap.min.js

    0 讨论(0)
  • 2020-12-02 06:05

    I feel that this will be simple JS fix:

    //for close, opened dropdown.
    $(".nav a").click(function () {
        if ($(".navbar-collapse").hasClass("in")) {
            $('[data-toggle="collapse"]').click();
        }
    });
    

    If this code is not working correctly then see that it's binding correctly, so place it in controller that loads page.

    0 讨论(0)
  • 2020-12-02 06:10

    Here is a working implementation using the ui.bootstrap.collapse module. https://jsfiddle.net/7z8hLuyu/

    HTML

    <div ng-app="app">
    <nav class="navbar navbar-default">
      <div class="container-fluid" ng-controller="NavigationCtrl as vm">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" ng-click="vm.toggleCollapse()">
            <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="#">Brand</a>
        </div>
    
        <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="navbar-collapse" style="overflow:hidden!important;" uib-collapse="vm.    isCollapsed">
          <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></    li>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 3</a></li>
            <li><a href="#">Link 4</a></li>     
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    </div>
    

    Controller (using controllerAs syntax):

    (function(){
        'use strict';
    
      angular
        .module('app', ['ngAnimate','ui.bootstrap.collapse'])
        .controller('NavigationCtrl', NavigationCtrl);
    
      NavigationCtrl.$inject = [];
    
      function NavigationCtrl() {
        var vm = this;
        vm.isCollapsed = true;
        vm.toggleCollapse = toggleCollapse;
    
        function toggleCollapse() {
            vm.isCollapsed = !vm.isCollapsed;
        }
      }
    
    })();
    

    Note: for animation to work in ui.bootstrap modules, you must include ngAnimate.

    0 讨论(0)
提交回复
热议问题