参见英文答案 >
How can I select an element in a component template?10个
在我的home.html:
在我的home.html:
<p #pmessage>msg 1</p> <p #pmessage>msg 2</p> <p #pmessage>msg 3</p> <p #pmessage>msg 4</p>
在我的家里:
export class HomePage { @ViewChildren('pmessage') pMessages; constructor() { //using first works,result <p>msg 1</p> console.log(this.pMessages.first.nativeElement); //using last also works,result <p>msg 4</p> console.log(this.pMessages.last.nativeElement); //How can I get the two in the middle? i.e <p>msg 2</p> and <p>msg 3</p> //this isn't working console.log(this.pMessage[1].nativeElement); //this either isn't working console.log(this.pMessage.1.nativeElement); } }
请帮忙.谢谢.
访问pMessages时有拼写错误. ‘s’缺失了.
原文链接:https://www.f2er.com/angularjs/141782.html`console.log(this.pMessages[1].nativeElement);`
您还应该在ngAfterViewInit或更高版本中访问viewChildren.在此之前,变量可能未定义