加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2 – 在导航点击时隐藏bootstrap导航栏

发布时间:2020-12-17 17:46:35 所属栏目:安全 来源:网络整理
导读:在移动设备中,单击引导程序导航栏项不会隐藏菜单. 我的菜单按钮,显示手机: button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar" 搜索此问题返回结果未发布到角度2,我不知道如何实现它们. 当用户点击链接时,如何隐
在移动设备中,单击引导程序导航栏项不会隐藏菜单.

我的菜单按钮,显示手机:

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">

搜索此问题返回结果未发布到角度2,我不知道如何实现它们.

当用户点击链接时,如何隐藏导航栏?

<li><a routerLink="/page">Click this should hide nav</a></li>

解决方法

您可以添加一个自定义指令来执行此操作

import { Directive,ElementRef,Input,HostListener } from "@angular/core";

@Directive({
    selector: "[menuClose]"
})
export class CloseMenuDirective {
    @Input()
    public menu: any;

    constructor(private element: ElementRef) { }

    @HostListener("click")
    private onClick() {
        this.menu.classList.remove("show");
    }
}

不要忘记将它添加到声明数组中的app.module

import { CloseMenuDirective } from './directives/close-menu.directive';
@NgModule({
    declarations: [
        ...declarations,CloseMenuDirective
    ]
})
export class AppModule { }

然后在HTML中创建对菜单的引用并将其传递给link元素.

<div class="page-layout">
    <!-- Mark the menu with #menu,thus creating a reference to it -->
    <aside class="collapse navbar-toggleable page-menu" id="navbar-header" #menu>
        <ul class="nav">
            <li class="nav-item">
                <a class="nav-link"
                   [routerLink]="['./somewhere']"
                   routerLinkActive="active"
                   menuClose      <!-- Our custom directive above -->
                   [menu]="menu"> <!-- Bind to menu -->
                    <span>My Link</span>
                </a>
            </li>
        </ul>
    </aside>
</div>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读