0
請幫助提出指令的可見性。在模板中,我有如下指令: <h1 *appTest visibility="true" id="myDirective">navigation:</
H1>如何使指示器顯示開關?
該指令通可視性VAR到:
import { Directive, Input, OnInit, ViewContainerRef, TemplateRef } from '@angular/core';
@Directive({
selector: '[appTest]'
})
export class TestDirective implements OnInit {
@Input() visibility: boolean;
constructor(private viewContainer: ViewContainerRef, private template: TemplateRef<Object>) {
}
ngOnInit() {
console.log(this.visibility);
if(this.visibility) {
this.viewContainer.createEmbeddedView(this.template);
} else {
this.viewContainer.clear();
}
}
}
但是控制檯顯示:
不確定
而且#myDirective是不在瀏覽器上顯示。我需要顯示並隱藏#myDirective,具體取決於屬性「visibility」的值
您是否嘗試爲可見性設置默認值visibility:boolean = false? – zgue
我需要從屬性 –
獲得可見性參數。通過在模板中使用'ngIf'指令併爲其分配一個動態'boolean'變量,可以直接顯示/隱藏元素。然而,這種方法將從DOM中完全刪除相應的模板(不僅僅保留'visibility:hidden',只隱藏它在頁面上,在DOM中保留該元素的空間)。你只是想根據你的輸入變量切換元素的可見性或者從DOM中完全添加/刪除元素嗎? – amal