Update innerhtml of element using viewchild in angular 2

拟墨画扇 提交于 2019-12-10 11:03:24

问题


I have html element like

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

I access the element in component using

@ViewChild('hiddenElement') hiddenElement: ElementRef;

How do i update the innerhtml of element whose class is value?


回答1:


You can get hiddenElement children using nativeElement.children, filter them by className and modify their innerHTML using element.innerHTML

Something like this

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

plnkr



来源:https://stackoverflow.com/questions/45667715/update-innerhtml-of-element-using-viewchild-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!