8

In my home.html:

<p #pmessage>msg 1</p>
<p #pmessage>msg 2</p>
<p #pmessage>msg 3</p>
<p #pmessage>msg 4</p>

In my home.ts:

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); 
    }
}

Please help. Thanks.

Vicheanak
  • 6,444
  • 17
  • 64
  • 98

1 Answers1

2

There is typo in accessing pMessages. The 's' is missing.

`console.log(this.pMessages[1].nativeElement);`

Also you should access viewChildren in ngAfterViewInit or later. The variable could be undefined before that

Arpit Agarwal
  • 3,993
  • 24
  • 30
  • Your solution is not working. Please see my plunkr: http://plnkr.co/edit/yuYQz7Mt1KGfzpWV0GrK?p=preview – Vicheanak Jul 01 '16 at 15:13