Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript code number likes by typescript / angular

I have some javascript code which I’m trying to add to my component.ts in my angular project.

Here is the code:

ngOninit() {
  let areaNum = document.getElementsByClassName("some-area").length;

  // The issue is in the code below:
  for (let area of document.getElementsByClassName("some-area")) {
    area.style.height = 100 / areaNum + "%";
  }
}

The error is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Type 'HTMLCollectionOf<Element>' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)

How can I fix this so it work in Angular?

>Solution :

Document.getElementsByClassName returns a live HTMLCollection. MDN link

You can make it into an array using Array.from

Array.from(document.getElementsByClassName("some-area"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading