我有一個組件打開了一個模塊,它基於如果一個名爲'isVisible'的變量是True或False。一旦模態可見,我想添加一個類到頁面的「body」標記中,一旦它被關閉,我想從「body」標記中移除該類。在Angular2的組件外添加一個類到一個特定的dom中
下面是我的代碼片段和我試過的。
import {Component, ElementRef} from '@angular/core';
@Component({
selector: 'modal',
template: '<div class="modal_div">
<div (click)="closeCard()"></div>
<div class="modal_body">blah</div>
</div>'
})
export class DailogComponent{
isVisible: boolean;
constructor(public element: ElementRef){
this.isVisible = false;
}
OpenModal(){
this.isVisible = true;
//add css class to body here which is where I am lost
console.log(this.element.nativeElement.parentNode.querySelector('body'));
}
closeModal(){
this.isVisible = false;
//remove css class to body here which is where I am lost
console.log(this.element.nativeElement.parentNode.querySelector('body'));
}
}
告訴我,如果我的回答是,你在等待什麼。這是一個奇怪的問題,因爲您已經具備了自己找到所需的知識。或者,也許我的回答顯然是錯誤的,在這種情況下,我會自己學習一些東西。 – Ced