1

In Angular 2, @ViewChildren can be used to get multiple HTML elements using local template variables, like

@ViewChildren('var1, var2, var3')

Is there a better and simple way to get multiple HTML elements like using a class name, for example:

@ViewChildren('my-sensitive-info-class')

to get all the elements containing the class name. Or using element type, name, ...

I tried to use ElementRef, but it is not working when HTML elements are embedded inside *ngIf.

I tried to set the same local template variables for different elements, and looks like @ViewChildren('myVar') can get all the elements for me. But Angular Doc says doing this has unknown consequences.

I have no choice but to use @ViewChildren because of async. But I want to simplify the @ViewChildren call. Otherwise, whenever I added a new elements, I need to add a new local template variable for the element, and them add the variable to @VeiwChildren(). If I can query based on class, then I only need to mark the new element with the class name. No need to change @ViewChildren() call.

user3616544
  • 1,023
  • 1
  • 9
  • 31

0 Answers0