Angular *ngFor to repeat buttons with list items

I am building a web App using Angular 2 and want to loop the button with the list items but outside of list items, how can I get the functionality using *ngFor, please help.

My HTML code:

<div class="col-xs-6">
            <a class="list-group-item clearfix" style="background-color:rgb(3, 0, 48)" *ngFor="let buying of buy">
                <div class="pull-left" style="max-width:350px">
                    <h5 style="color:white">{{buying.names}}</h5>
                    <p style="color:white">{{buying.desc}}</p>
                </div>
                    <div>
                        <span class="pull-right" >
                        <img [src]="buying.getImg" alt="image not loaded" class="img-responsive" style="max-height:100px">
                    </span>
                </div>
            </a>
        </div>  
        <div class="col-xs-6"> <-----I WANT TO LOOP THIS BUTTON
                <button class="btn btn-primary ; pull-right">Add To Cart</button>
            </div> 

1 answer

  • answered 2018-01-14 10:58 Lakindu Gunasekara

    Try this code snippet. I have created a div tag above linking both content and then added the loop inside that, so it will refer to both elements (item and button).

    <div *ngFor="let buying of buy">
      <div class="col-xs-6">
        <a class="list-group-item clearfix" style="background-color:rgb(3, 0, 48)">
          <div class="pull-left" style="max-width:350px">
            <h5 style="color:white">{{buying.names}}</h5>
            <p style="color:white">{{buying.desc}}</p>
          </div>
          <div>
            <span class="pull-right">
              <img [src]="buying.getImg" alt="image not loaded" class="img-responsive" style="max-height:100px">
            </span>
          </div>
        </a>
      </div>
    
      <div class="col-xs-6">
        <button class="btn btn-primary ; pull-right">Add To Cart</button>
      </div>
    </div>