Angular2 Toggle Class in child component using a click event in parent component

Angular2 Toggle Class in child component using a click event in parent component

我正在尝试使用点击事件切换 class on/off,我 运行 遇到的问题是我的点击事件在 parent 组件中而 Class 在 child 组件中。我可以使用 @Input() 装饰器用 class 初始化 child 但之后我无法切换 class

import { Component, AfterViewInit, Input, OnInit } from '@angular/core';
import { SidebarComponent } from './components/sidebar/sidebar.component';

@Component({
   moduleId: module.id,
   selector: 'body',
   host: {
      '[class.navMdClass]' : 'navMdClass',
      '[class]' : 'classNames'
   },
   templateUrl:'app.component.html' ,
   directives:[SidebarComponent],    
})
export class AppComponent implements OnInit {
   private isClassVisible:boolean;
   constructor () {    

   }
   ngOnInit() {
      this.isClassVisible=true;
   }
   toggleClass() {
      this.isClassVisible = !this.isClassVisible;       
   }
}
//Child component
import { Component, Input, EventEmitter, OnInit,SimpleChange } from '@angular/core';
@Component({
    moduleId: module.id,
    selector: 'sidebar',
    templateUrl:'sidebar.component.html' 
})

export class SidebarComponent implements OnInit{
    @Input() isClassVisible:boolean;

ngOnInit() { }
}
//Parent click event triggers toggleClass
<div class="nav toggle">
          <a (click)="toggleClass();" href="#" id="menu_toggle"><i class="fa fa-bars"></i></a>
      </div>
//Child div where target class is
<div class="left_col" [class.scroll-view]="[isClassVisible]" >

您需要删除 [class.scroll-view]="[isClassVisible]" 到

的大括号
[class.scroll-view]="isClassVisible"

我找到了该问题的解决方案,该解决方案涉及将 class 名称作为属性传递给子组件。如果你有多个 classes 传递到不同的位置,你可以传递一个数组 属性 并在适当的区域输出。

//parent component
import { Component, AfterViewInit, Input, OnInit } from '@angular/core';
import { SidebarComponent } from './components/sidebar/sidebar.component';

@Component({
   moduleId: module.id,
   selector: 'body',
   host: {
      '[class.navMdClass]' : 'navMdClass',
      '[class]' : 'classNames[0]'
   },
   templateUrl:'app.component.html' ,
   directives:[SidebarComponent]


})
export class AppComponent{

   navMdClass:boolean;
   classNames:Array<any>;

   constructor () {
      this.navMdClass=false;

      this.classNames = ['nav-md', 'scroll-view',  'sidebar-footer'];
   }

   toggleClass() {

      this.navMdClass = !this.navMdClass;
      if(this.navMdClass==false){ this.classNames[0] = 'nav-md'; this.classNames[1]="scroll-view"; this.classNames[2]="sidebar-footer"}
      if(this.navMdClass==true){  this.classNames[0] = 'nav-sm'; this.classNames[1]=""; this.classNames[2]="sidebar-footer-hide"}
      //console.log(this.classNames[0]);
   }
}
//child component
import { Component, Input, EventEmitter, OnInit, SimpleChange, OnChanges  } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'sidebar',
    templateUrl:'sidebar.component.html',
    properties: ['cls']
})

export class SidebarComponent{
    @Input() classNames:Array<any>;
}

//Parent click event triggers toggleClass
<div class="nav toggle">
          <a (click)="toggleClass();" href="#" id="menu_toggle"><i class="fa fa-bars"></i></a>
      </div>
//Sidebar selector in parent is where you pass the cls property declared in child component and pass it the classNames array from the parent component
<sidebar [cls]="classNames"></sidebar>
//Child div where target class is
<div class="left_col {{cls[1]}}" >
<div class="{{cls[2]}}" >