在我的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);
}
}
请帮忙。感谢。
答案 0 :(得分:2)
访问pMessages
时会出现拼写错误。 's'缺失了。
`console.log(this.pMessages[1].nativeElement);`
您还应该在ngAfterViewInit
或更高版本中访问viewChildren。在此之前,变量可以是undefined