1

I am writing an Angular component that has a QueryList of HTMLElements and when iterating over the list, I am able to log each element to console. However when I try to access a property of the element it returns undefined. This is true for every property of the element. I have made sure that the elements are actually present in the list, but they are present as objects with the property 'nativeElement' Here is the console

Thank you for considering this.


@ViewChildren('element') elements !: QueryList<HTMLElement>

function()
  {
    this.elements.forEach(function(element) {
      console.log(element) //prints object with correct data to console
      console.log(element.className) //prints undefined
    })
  }

2 Answers 2

2

Hey @Drew have you tried in this way I have shown below as nativeElement object which exposes all the methods and properties of the native elements in angular. Replace QueryList<HTMLElements> with QueryList<ElementRef> then do as shown below.

console.log(element.nativeElement.className);
Sign up to request clarification or add additional context in comments.

Comments

1

You need to change the typescript in @viewchildren to QueryList<ElementRef> like this:

  @ViewChildren("element") elements!: QueryList<ElementRef>;

Than you can see the autocomplete of the element in the correct way -

console.log(element.nativeElement.className);

Here is an example on StackBlitz

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.