有没有办法在ng2-bootstrap中构建移动导航栏? [英] Is there a way to build the mobile nav bar in ng2-bootstrap?

查看:48
本文介绍了有没有办法在ng2-bootstrap中构建移动导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在实现ng2-bootstrap和Angular2.

I've been implementing ng2-bootstrap and Angular2.

我不知道如何打开/关闭移动导航栏.

I cannot figure out how to make the mobile navbar open / close.

这是尚不支持的功能吗?还是我想念什么?

更新,html:

<nav class="navbar navbar-default">
    <div class="container-fluid">

    <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <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="#">
            <img src="/logo.png" />
        </a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
            <li router-active>
                <a [routerLink]=" ['Index'] ">Summary<span class="sr-only">(current)</span></a>
            </li>
            <li router-active>
                <a [routerLink]=" ['Portfolio'] ">Portfolio<span class="sr-only">(current)</span></a>
            </li>
            <li router-active>
                <a [routerLink]=" ['About'] ">About<span class="sr-only">(current)</span></a>
            </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 dropdown keyboardNav="true">
                <a href class="dropdown-toggle" role="button" aria-expanded="false" dropdownToggle>
                    <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
                    Andrew Duncan 
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu" role="menu">
                    <li role="menuitem"><a class="dropdown-item" href="#">Account Settings</a></li>
                    <li role="menuitem"><a class="dropdown-item" href="#">Another action</a></li>
                    <li role="menuitem"><a class="dropdown-item" href="#">Something else here</a></li>
                    <li class="divider dropdown-divider"></li>
                    <li role="menuitem"><a class="dropdown-item" href="#">Logout</a></li>
                </ul>
            </li>

        </ul>
    </div>
</div>

打字稿:

/*
 * Angular 2 decorators and services
 */
import { Component, ViewEncapsulation } from '@angular/core';
import { RouteConfig, Router } from '@angular/router-deprecated';

import { AppState } from './app.service';
import { Home } from './home';
import { RouterActive } from './router-active';
import { BUTTON_DIRECTIVES, DROPDOWN_DIRECTIVES } from '../../node_modules/ng2-bootstrap';
/*
 * App Component
 * Top Level Component
 */
@Component({
  selector: 'app',
  pipes: [ ],
  providers: [ ],
  directives: [ 
    RouterActive, 
    BUTTON_DIRECTIVES,
    DROPDOWN_DIRECTIVES ],
  encapsulation: ViewEncapsulation.None,
  styles: [
    require('./app.css')
  ],
  template: require('./app.html')
})
@RouteConfig([
  { path: '/',      name: 'Index', component: Home, useAsDefault: true },
  { path: '/home',  name: 'Home',  component: Home },
  // Async load a component using Webpack's require with es6-promise-loader and webpack `require`
  { path: '/about', name: 'About', loader: () => require('es6-promise!./about')('About') },
  { path: '/portfolio', name: 'Portfolio', loader: () => require('es6-promise!./portfolio')('Portfolio') }
])
export class App {
  angularclassLogo = 'assets/img/angularclass-avatar.png';
  loading = false;
  url = 'https://twitter.com/AngularClass';

  constructor(
    public appState: AppState) {

  }

  ngOnInit() {
    console.log('Initial App State', this.appState.state);
  }

}

/*
 * Please review the https://github.com/AngularClass/angular2-examples/ repo for
 * more angular app examples that you may copy/paste
 * (The examples may not be updated as quickly. Please open an issue on github for us to update it)
 * For help or questions please contact us at @AngularClass on twitter
 * or our chat on Slack at https://AngularClass.com/slack-join
 */

推荐答案

您需要包含并使用折叠指令

首先,您导入指令
import { CollapseDirective } from 'ng2-bootstrap'

将其包含在组件指令中
@Component({ directives: [CollapseDirective] })

正如Akkusativobjekt在评论中指出的那样,指令(在angular 2的当前稳定版本中)不再放置在@Component属性中.
它们包含在NgModule属性中.

@NgModule({ declarations: [CollapseDirective] })

You need to include and use the collapse directive

first you import the directive
import { CollapseDirective } from 'ng2-bootstrap'

Include it in your components directives
@Component({ directives: [CollapseDirective] })

As pointed out by Akkusativobjekt in the comments, Directives (in the current stable version of angular 2) are no longer placed in the @Component attribute.
They are included in the NgModule attribute.

@NgModule({ declarations: [CollapseDirective] })

然后在您的控制器中创建一个变量以跟踪其是否折叠
export class MyController { public isCollapsed: boolean = true; }

并在模板中显示如下所示的行
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" >
您需要切换变量
<button type="button" class="navbar-toggle collapsed" (click)="isCollapsed = !isCollapsed" >

此外,您还需要在模板中更改以下行:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 包含指令
<div id="navbar" class="navbar-collapse collapse" [collapse]="isCollapsed">

有关ng2-bootstrap崩溃的文档
在html中使用崩溃指令的示例
NgModule的文档

then in your controller create a variable to keep track of whether or not it's collapsed
export class MyController { public isCollapsed: boolean = true; }

and in your template the line that looks like this
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" >
you'll want to toggle the variable
<button type="button" class="navbar-toggle collapsed" (click)="isCollapsed = !isCollapsed" >

And also in your template you'll want to change the line that says
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> to include the directive
<div id="navbar" class="navbar-collapse collapse" [collapse]="isCollapsed">

Documentation for ng2-bootstrap collapse
Example of using the collapse directive in html
Documentation for NgModule

这篇关于有没有办法在ng2-bootstrap中构建移动导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆