我想在点击按钮时使用类myBLock在div中添加一个组件
code:
<div class="myExample">
<button (click)="addComponent()">
</button>
</div>
<div class="myBlock">
</div>
如果要添加html:
HTML
<div class="myBlock" #block>
</div>
在组件中:
@ViewChild('block') block:ElementRef;
addComponent() {
block.nativeElement.insertAdjacentHTML('beforeend', '<div></div>');
}
如果要添加组件,则需要动态执行此操作:
您可以根据按钮单击将组件变量设置为true / false。然后根据变量值显示/隐藏组件。
<div>
<button (click)='showMyBlock = !showMyBlock'>
</button>
</div>
<another-component *ngIf='showMyBlock'>
</another-component>