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