4

I want to dynamically add divs using a loop with typescript in angular. I want to reproduce the following pseudocode in my .ts component file:

i: number = 4;
arrayofHTMLelements: html elements = [];

for i in range (1, i):
    create div at index i

I'm assuming I could then slide the array of HTML elements into the .html component file using:

<li *ngFor="let arrayofHTMLelement of arrayofHTMLelements; let i = index">{{i + 1}}: {{arrayofHTMLelements}}</li>

1 Answer 1

4

You should iterate over your objects and create the inner html in the template like this:

const heroes = [
  { name: 'Spiderman' },
  { name: 'Superman' },
  { name: 'Superwoman!' }
]

template: `
  <ul>
    <li *ngFor="let hero of heroes">
      <div>{{hero.name}}</div>
    </li>
  </ul>
`
Sign up to request clarification or add additional context in comments.

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.