[英]How do I remove a DOM element or empty it from a component
I have a dom element, generated automatically by an external component. 我有一个dom元素,它是由外部组件自动生成的。
<div id="abc">test data</div>
I would like to remove, hide or empty the content, whichever is easier. 我想删除,隐藏或清空内容,以较容易的为准。
Your request is quite odd, because before ngOnInit()
nothing is rendered yet. 您的请求很奇怪,因为在
ngOnInit()
之前没有呈现任何内容。 So I guess you want it to be hidden on loading of the component? 因此,我想您希望在组件加载时将其隐藏? You can use
*ngIf
: 您可以使用
*ngIf
:
<div *ngIf="showTestData" id="abc">test data</div>
@Component({})
export class TestComponent {
showTestData: boolean = false;
ngOnInit(): void {
// nothing necessary here
}
}
<div id="abc"><span *ngIf="show_it">test data</span></div>
And you declare show_it
as a member variable of the component class, and set it to false in ngOnInit. <div id="abc"><span *ngIf="show_it">test data</span></div>
然后,将show_it
声明为组件类的成员变量,并在ngOnInit中将其设置为false。
Of course, you can put the ngIf
on the div
directly. 当然,您可以将
ngIf
直接放在div
。
Edit after your comments If you know the id for sure and it comes from outside, you can use @ViewChild
to select the element and hide it once you get a hold of it. 在评论后进行编辑如果您确定ID确实是从外部
@ViewChild
,则可以使用@ViewChild
选择该元素,并在保留该元素@ViewChild
其隐藏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.