繁体   English   中英

使用Angular 2中的viewchild更新元素的innerhtml

[英]Update innerhtml of element using viewchild in angular 2

我有喜欢的html元素

<section class="" #hiddenElement>
    <span>title</span>
    <span class="value"></span>
</section>

我使用访问组件中的元素

@ViewChild('hiddenElement') hiddenElement: ElementRef;

如何更新其类为value的元素的innerhtml?

您可以使用nativeElement.children获取hiddenElement nativeElement.children ,通过className对其进行过滤,并使用element.innerHTML修改其innerHTML

像这样

Array
    .from(this.hiddenElement.nativeElement.children)
    .filter(c => c.className.includes('value'))
    .forEach(c => c.innerHTML = 'New innerHTML');

plnkr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM