0

I have a component which allows creating buttons and textboxes on click of corresponding button. Is there any way of accessing newly added elements? I have tried using ViewChildren and subscribing to the changes, but of no use. I tried with injecting elementref in constructor, but I want the ViewChildren to work properly i.e. the querylist to be updated.

Here is the link for the stackblitz example - https://stackblitz.com/edit/angular-w69vso

Sreenath
  • 337
  • 2
  • 9

1 Answers1

1

The code

@ViewChildren("input")

means that you're going to query elements which have #input template reference variables but you use <input #templateTextBox so i would try:

@ViewChildren("templateTextBox")

instead.

Forked Stackblitz

See also:

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • That's really silly of me :| But is there any way of accessing the element by type.. i.e. get all the buttons using the ViewChildren? – Sreenath Jul 09 '18 at 08:23
  • 1
    We can't query by type but you can create directive which will have selector `button` and inject ElementRef in constructor and then just use `@ViewChildren(ThatDirective)` – yurzui Jul 09 '18 at 09:13