简体   繁体   English

如何在angular2中显示和隐藏侧面菜单

[英]How to show and hide side menu in angular2

In my application i have to implement hide and show side menu. 在我的应用程序中,我必须实现隐藏和显示侧边菜单。 By default the page menu is open while clicking the toggle menu i have to hide the side menu. 默认情况下,单击切换菜单时页面菜单是打开的,我必须隐藏侧面菜单。 How can i implement this. 我该如何实施呢?

what i have is: 我所拥有的是:

app.component.html, nav.component.html app.component.html,nav.component.html

 <div class="menu-toggler sidebar-toggler"> <span></span> </div> <ul> <li class="nav-item "> <a class="nav-link nav-toggle"> <i class="icon-diamond"></i> <span class="title">Name</span> <span class="arrow"></span> </a> </li> </ul> 

Myservice.ts 我的服务

export class GlobalService {
public collapse;

constructor() { }

setValue(val: boolean) {
    this.collapse = val;
}

getValue() {
    return this.collapse;
}

EDIT 编辑

app.component.html app.component.html

<div *ngIf="!toggle()"class="menu-toggler sidebar-toggler">
      <span></span>
</div>

app.component.ts app.component.ts

import { GlobalService } from "path";

export class AppComponent {
toggle() {
this.globalService.setValue(false);

 }
}

how can i hide this list(in nav.html) while clicking menu toggle (app.compnent.html)? 如何在单击菜单切换(app.compnent.html)时隐藏此列表(在nav.html中)? Any help will really appreciable. 任何帮助将非常可观。 i am new to angular. 我是新来的角度。

If use of service is not the priority then you can simply maintain simple variable to do this task. 如果不优先使用服务,则只需维护简单变量即可完成此任务。

Your app.component.ts 您的app.component.ts



    export class AppComponent {
      showMenu : boolean = true;
    }

Your app.component.html 您的app.component.html

<div (click)="showMenu = !showMenu" class="menu-toggler sidebar-toggler"><span></span>
</div>
<ul *ngIf="showMenu">
    <!-- used showMenu to hide/show -->
    <li class="nav-item  ">
        <a class="nav-link nav-toggle">
            <i class="icon-diamond"></i>
            <span class="title">Name</span>
            <span class="arrow"></span>
        </a>
    </li>
</ul>

hope this helps ... 希望这可以帮助 ...

For this , 为了这 ,

  1. You can make a CommonService to store the state of menu or and use that Service to make toggle you menu. 您可以创建一个CommonService来存储菜单的状态,也可以使用该Service来切换菜单。

  2. You can also use @Input @Output , in case you are having parent child relation between components. 您还可以使用@Input @Output,以防组件之间存在父子关系。

Method will depend on how is your project/file structure. 方法将取决于您的项目/文件结构。

You can create a service and preferably make a static variable inside to get and set the visibility state of the menu. 您可以创建服务,并且最好在其中创建一个静态变量以获取并设置菜单的可见性状态。 By this you could directly set and get the variable by using ComponentName.variableName. 这样,您可以使用ComponentName.variableName直接设置和获取变量。

to play with the visibility you could use(Sorry if there is any syntax errors) 1> Set the document.getelementbyid("idofelement").display= none or block 2>use *ngIf="someboolean" where you should set the boolean in your ts file 发挥您可以使用的可见性(对不起,如果有任何语法错误)1>设置document.getelementbyid(“ idofelement”)。display = none或block 2>使用* ngIf =“ someboolean”设置布尔值在您的ts文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM