1 回答

TA貢獻1775條經驗 獲得超8個贊
我能夠通過創(chuàng)建嵌套數(shù)組來實現(xiàn)這一點。
從服務器獲取數(shù)據(jù)
檢查屏幕尺寸以決定在輪播上顯示多少張圖片
將數(shù)組中的數(shù)據(jù)和圖像數(shù)量發(fā)送到塊方法
在 html 中申請循環(huán)
HTML
<div class="carousel-item row w-100 mx-3 text-center {{ i == 0 ? 'active' : '' }} m-t-0" style="margin-right: 70px;" *ngFor='let fav of userFavourite; let i = index' >
<div class=" d-flex justify-content-around w-100" >
<div class="mainSlide " style="align-content: center;" *ngFor="let x of fav;" >
///Enter each image from loop..etc
</div>
</div>
</div>
打字稿/Component.ts
//get the image/data from server
getUserFavourite() {
this._productService.getUserFavourite(this.loggedInUsername).subscribe(
(res) => {
if( this.scrWidth>1300){
this.favCount=4;
} else if(this.scrWidth<1300 && this.scrWidth>1025){
this.favCount='3';
} else if(this.scrWidth<1025 ){
this.favCount='2';
}
else if(this.scrWidth<600 ){
this.favCount='1';
}
this.userFavourite = this.chunks(res,this.favCount);
console.log(this.userFavourite);
},
(err) => {
this.error = err;
}
);
}
/**************************************************************** */
//gets the size of window screen to adjust number of images in an array to fit carousel
@HostListener('window:resize', ['$event'])
getScreenSize(event?) {
this.scrHeight = window.innerHeight;
this.scrWidth = window.innerWidth;
console.log(this.scrHeight, this.scrWidth);
}
/******************************************************************** */
//adds images from server to array
chunks(array, size) {
let results = [];
results = [];
while (array.length) {
results.push(array.splice(0, size));
}
return results;
}
添加回答
舉報