Finding height of the largest div element and setting it for all div elements Angular 8

后端 未结 1 1759
终归单人心
终归单人心 2021-01-24 23:47

I have a div element that is looped through an array of objects using *ngFor. The height of a particular div is different from each other as each object contains different amoun

相关标签:
1条回答
  • 2021-01-25 00:03

    You can use ViewChildren to get the elements, and in ngAfterViewInit, get the size

    <div class="col-md-4" *ngFor="let food of foods">
      <div #foodcard class="food-card">
         <h4>{{food.title}}</h4>
         <p>{{food.ingredients}}</p>
      </div>
    </div>
    
    @ViewChildren('foodcard') foodCards:QueryList<ElementRef>
    ngAfterViewInit()
    {
       let maxHeight=0
       this.foodCards.forEach(x=>{
         const height=x.nativeElement.getBoundingClientRect().height
         if (heigth>maxHeight)
            maxHeight=height
       })
    
    }
    

    Anyway, if your aim is set the same height check card-deck

    0 讨论(0)
提交回复
热议问题