我试图访问的值span由下式给出其值ng-content。
我在模板中有一个包含此html的按钮组件:
<span #text class="o-button-label">
<ng-content></ng-content>
</span>
Run Code Online (Sandbox Code Playgroud)
所以我可以
<my-button>Hello</my-button>
Run Code Online (Sandbox Code Playgroud)
转变为
<span #text class="o-button-label">
Hello
</span>
Run Code Online (Sandbox Code Playgroud)
现在,我希望该文本能够在.NET中也能使用aria-label。除其他外,我在my-button组件中尝试了以下操作:
@ViewChild('text') textElement: ElementRef;
ngAfterViewInit() {
console.log('textElement', this.textElement.nativeElement.value);
}
Run Code Online (Sandbox Code Playgroud)
但这总是给我undefined。value用nodeValue退货代替null。我检查了this.textElement.nativeElement哪个是span预期的,span包含预期的值,但是我找不到找到该文本值的方法。
我所做的所有搜索都指向与我一样的用法@ViewChild,但是总是要获取一些组件或DOM节点(例如在这个好主题中),而不仅仅是获取文本,因此是否有可能获取由注入的文本值ng-content?
angular ×1