简体   繁体   中英

How do you create a dynamic number of divs with dynamic content in Angular2?

I know you would use ngFor, but I'm not quite sure how to do it.

Basically I want want to create the div below n times, replacing the 0 with 1 for the 2nd div, 2 for the 3rd div, and so on...

<div class="box">
    <p class="dut-text dut-title">
        {{list[0].data}}
    </p>
</div>

Creates below div for each value in items :

<div class="box" *ngFor="let item of items;let i=index">
    <p class="dut-text dut-title">
        {{list[i].data}}
    </p>
</div>
class MyComponent {
  items = ['a', 'b', 'c'];
}

Update

alternative interpretation of your question (see comments)

<div class="box" *ngFor="let item of list">
    <p class="dut-text dut-title">
        {{item.data}}
    </p>
</div>
class MyComponent {
  list = ['a', 'b', 'c'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM