header.component.html有一個按鈕,當您點擊菜單桅杆顯示在users.component.html。點擊添加類到按鈕。如何添加類菜單塊,當點擊標題按鈕(沒有jQuery)?Angular 2點擊打開菜單
header.component.ts
import {Component} from '@angular/core';
import {Router} from "@angular/router";
import {GlobalService} from "../../global.service";
@Component({
selector: 'header',
providers: [GlobalService],
templateUrl: 'app/_components/header/header.component.html'
})
export class HeaderComponent{
public activeMobileMenuAdmin = false;
public activeClass = false;
constructor(public router: Router, public globalService: GlobalService){
router.events.subscribe((val) => {
if (val.url === '/login' || val.url === '/users') {
this.adminPanelView = false;
} else {
this.adminPanelView = true;
}
if (val.url === '/users'){
this.adminMenuView = true;
this.detectDeviceWidth();
} else {
this.adminMenuView = false;
}
});
this.activeClass = globalService.activeClass;
}
admMenuShow(){
this.activeClass = !this.activeClass;
}
ngOnInit() {
this.detectDeviceWidth();
}
detectDeviceWidth() {
if (window.innerWidth < 1024) {
this.activeMobileMenuAdmin = true;
} else {
this.activeMobileMenuAdmin = false;
}
}
}
header.component.html
<div class="menu-icon show-on-sm" [ngClass]="{'active': activeClass}" (click)="admMenuShow();" *ngIf="adminMenuView">
<span></span>
<span></span>
<span></span>
users.component.ts
個import {Component} from '@angular/core';
import {GlobalService} from "../../global.service";
@Component({
selector: 'admin',
providers: [GlobalService],
templateUrl: 'app/admin/users/users.component.html'
})
export class AdminUsersComponent {
private activeClass = true;
constructor(public globalService: GlobalService){
this.activeClass = globalService.activeClass;
}
admMenuShow(){
this.activeClass = !this.activeClass;
}
}
users.component.html
<div class="menu" id="admin-menu" [ngClass]="{'active': activeClass}">
<div class="contflex">
<div class="h1">Test</div>
<ul>
<li class="active">List 1</li>
<li>List 2</li>
<li>List 3</li>
</ul>
</div>
global.service.ts
import {Injectable} from '@angular/core';
import {Router} from '@angular/router';
@Injectable()
export class GlobalService {
public user: Object = {};
public hideMenu: boolean = true;
public activeClass: boolean = false;
constructor(public _router: Router) {}
admMenuShow(){
return this.activeClass = !this.activeClass;
}
onAuthError() {
console.log('Works!');
}
}
頁結構:
<header>
...
<div class="menu-icon show-on-sm" [ngClass]="{'active': activeClass}" (click)="admMenuShow();" *ngIf="adminMenuView">
<span></span>
<span></span>
<span></span>
</div>
...
</header>
<main>
<router-outlet></router-outlet>
<admin>
...
<div class="menu" id="admin-menu" [ngClass]="{'active': activeClass}">
<div class="contflex">
<div class="h1">Menu</div>
<ul>
<li class="active">List 1</li>
<li>List 2</li>
<li>List 3</li>
</ul>
</div>
</div>
...
</admin>
</main>
你是什麼意思的「菜單塊」? – bob
你可以創建一個工作plunker? – Aravind