Angular UI Bootstrap 响应式导航栏下拉菜单在新版本中无法正常工作

Posted

技术标签:

【中文标题】Angular UI Bootstrap 响应式导航栏下拉菜单在新版本中无法正常工作【英文标题】:Angular UI Bootstrap responsive Navbar dropdown not working properly in new version 【发布时间】:2015-04-24 09:45:45 【问题描述】:

我刚刚使用grunt bowerYeoman 创建了一个AngularJS 项目。包括更新版本为Angular 1.3.13。我想使用Angular UI 引导程序。我添加了响应式导航栏。但是在小屏幕上,下拉菜单似乎不起作用。当我点击下拉菜单时,整个菜单都会隐藏起来。

这是我的 index.html

<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<!-- build:css(.) styles/vendor.css -->
<!-- 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/main.css">
<!-- endbuild -->
</head>
<body ng-app="webClientApp">
    <!--[if lt IE 7]>
      <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 -->
    <header>
        <nav class="navbar navbar-default" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">

                <button type="button" class="navbar-toggle"
                    ng-init="navCollapsed = true"
                    ng-click="navCollapsed = !navCollapsed">
                    <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="#/">Web Client</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse"
                ng-class="!navCollapsed && 'in'" ng-click="navCollapsed=true">

                <ul class="nav navbar-nav">
                    <li class="active"><a href="">Link</a></li>
                    <li><a href="">Link</a></li>

                    <li class="dropdown"><a href="" class="dropdown-toggle" data-toggle="dropdown">Dropdown
                            <b class="caret"></b>
                    </a>
                        <ul class="dropdown-menu">
                            <li><a href="">Action</a></li>
                            <li><a href="">Another action</a></li>
                            <li><a href="">Something else here</a></li>
                            <li><a href="">Separated link</a></li>
                            <li><a href="">One more separated link</a></li>
                        </ul></li>

                </ul>
                <form class="navbar-form navbar-left" role="search">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="">Link</a></li>
                    <li class="dropdown"><a href="" class="dropdown-toggle"
                        data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a href="">Action</a></li>
                            <li><a href="">Another action</a></li>
                            <li><a href="">Something else here</a></li>
                            <li><a href="">Separated link</a></li>
                        </ul></li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </nav>
    </header>
    <div class="container">
        <div ng-view=""></div>
    </div>

    <div class="footer">
        <div class="container">
            <p>
                <span class="glyphicon glyphicon-heart"></span> from the Yeoman team
            </p>
        </div>
    </div>


    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
    <script>
        !function(A, n, g, u, l, a, r) 
            A.GoogleAnalyticsObject = l, A[l] = A[l] || function() 
                (A[l].q = A[l].q || []).push(arguments)
            , A[l].l = +new Date, a = n.createElement(g), r = n
                    .getElementsByTagName(g)[0], a.src = u, r.parentNode
                    .insertBefore(a, r)
        (window, document, 'script',
                '//www.google-analytics.com/analytics.js', 'ga');

        ga('create', 'UA-XXXXX-X');
        ga('send', 'pageview');
    </script>

    <!-- build:js(.) scripts/vendor.js -->
    <!-- bower: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-animate/angular-animate.js"></script>
    <script src="bower_components/angular-cookies/angular-cookies.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-touch/angular-touch.js"></script>
    <script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
    <!-- endbower -->
    <!-- endbuild -->

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

我正在使用所有最新版本的 JS。这是我的 bower.json


  "name": "web-client",
  "version": "0.0.0",
  "dependencies": 
    "angular": "^1.3.13",
    "bootstrap": "^3.3.2",
    "angular-animate": "^1.3.13",
    "angular-cookies": "^1.3.13",
    "angular-resource": "^1.3.13",
    "angular-route": "^1.3.13",
    "angular-sanitize": "^1.3.13",
    "angular-touch": "^1.3.13",
    "angular-bootstrap": "0.12.1"
  ,
  "devDependencies": 
    "angular-mocks": "^1.3.13"
  ,
  "appPath": "app",
  "moduleName": "webClientApp"

全屏导航栏工作正常。但是当我调整浏览器大小时,响应栏正在工作。 菜单只会在点击切换按钮时显示。但是当我点击下拉菜单时,整个菜单都隐藏了。

我关注了这个*** Question,我得到了一个Plunker Demo。它在那里工作得很好。

但我使用的是所有文件的最新版本。所以我的不工作。这里是my code what I tried in Plnker。

如何解决这个问题?谢谢。

【问题讨论】:

【参考方案1】:

您的下拉菜单部分已更改。

这是Plunker

<li dropdown>
     <a href="#" dropdown-toggle>Dropdown <b class="caret"></b></a>
     <ul class="dropdown-menu">
          <li><a href="#">...</a></li>
          ....
     </ul>
</li>

【讨论】:

所以它变成了属性而不是类?【参考方案2】:

针对 AngularJS 1.5+ 和 angular-ui-ootstrap 2.5+ 更新

我为 AngularJS 1.5+ 中的工作下拉菜单和菜单栏中的 UIBootstrap 尝试了几个选项 - 没有任何效果并且已经过时,所以我想我会在这里分享我的解决方案,也可以在 this repo commit 中找到。

我做了什么

    使用来自https://angular-ui.github.io/bootstrap/#!#collapse 的普通导航栏示例 将它与按钮下拉功能相结合,如https://angular-ui.github.io/bootstrap/#!#dropdown 示例中所示

代码(请参考上面的第 1 点和第 2 点以获得进一步的指导)

<nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" ng-click="isNavCollapsed = !isNavCollapsed">
                <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="#">ToDoSth</a>
    </div>
    <div class="collapse navbar-collapse" uib-collapse="isNavCollapsed">
        <ul class="nav navbar-nav">
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li uib-dropdown on-toggle="toggled(open)">
                <a href id="simple-dropdown" uib-dropdown-toggle>Link 3 <span class="caret"></span></a>
                <ul class="dropdown-menu" uib-dropdown-menu aria-labelledby="simple-dropdown">
                    <li><a href="#">SubItem 1</a></li>
                    <li><a href="#">SubItem 2</a></li>
                    <li><a href="#">SubItem 3</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">SubItem 4</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li uib-dropdown on-toggle="toggled(open)">
                <a href id="simple-dropdown" class="nav-text" uib-dropdown-toggle>Signed in as John Doe <span class="caret"></span></a>
                <ul class="dropdown-menu" uib-dropdown-menu aria-labelledby="simple-dropdown">
                    <li><a href="#">Profile</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">Sign Out</a></li>
                </ul>
            </li>
        </ul>
    </div>
</nav>

【讨论】:

【参考方案3】:

针对ui.bootstrap的最新版本,0.12.1,我觉得应该是ui.bootstrap的Dropdown和Collapse的组合解决方案:

    对于下拉菜单项,它在外部元素中使用“dropdown”,在内部元素中使用“dropdown-toggle”来触发下拉动作;

    <div class="btn-group" dropdown is-open="status.isopen">
      <button type="button" class="btn btn-primary dropdown-toggle" dropdown-toggle ng-disabled="disabled">
        Button dropdown <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li class="divider"></li>
        <li><a href="#">Separated link</a></li>
      </ul>
    </div>
    

    对于导航栏折叠按钮,在响应模式下,需要在导航栏切换按钮和导航栏div中添加“折叠”指令。

    <div ng-controller="CollapseDemoCtrl">
        <button class="btn btn-default" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button>
        <hr>
        <div collapse="isCollapsed">
            <div class="well well-lg">Some content</div> 
        </div>
    </div>
    

具体实现请参考:My Plunker

【讨论】:

感谢 Plunker!

以上是关于Angular UI Bootstrap 响应式导航栏下拉菜单在新版本中无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

在 webpack 中使用 angular-ui-bootstrap

如何在 Angular 1.5 中将 Angular 组件与 ui.bootstrap.modal 一起使用?

Bootstrap (4) 可以与 Angular Material (2) 一起集成吗?

我应该使用 Angular UI Bootstrap 还是普通的 Bootstrap 3? [关闭]

如何在打字稿中使用 angular-ui-bootstrap(模态)?

angular-ui-bootstrap 日期选择器和屏蔽输入