Closed Dragosp33 closed 10 months ago
Also, with this configuration for the component.ts
:
export class ProductsComponent implements OnInit {
public bikes_type: Bikes_type[];
customOptions: OwlOptions = {
autoplay: true,
smartSpeed: 1000,
margin: 45,
navSpeed: 600,
dots: false,
loop: true,
navText: [
'<i class="bi bi-arrow-left"></i>',
'<i class="bi bi-arrow-right"></i>',
],
responsive: {
0: {
items: 1,
},
768: {
items: 2,
},
992: {
items: 3,
},
1200: {
items: 3,
},
},
nav: true,
responsiveRefreshRate: 500,
};
constructor(private bikes_typeService: bikes_typeService) {
this.bikes_type = [];
}
ngOnInit() {
this.getBikes();
setTimeout(() => {
this.customOptions = {
autoplay: true,
smartSpeed: 1000,
margin: 45,
navSpeed: 600,
dots: false,
loop: true,
navText: [
'<i class="bi bi-arrow-left"></i>',
'<i class="bi bi-arrow-right"></i>',
],
responsive: {
0: {
items: 1,
},
768: {
items: 2,
},
992: {
items: 2,
},
1200: {
items: 2,
},
},
nav: true,
responsiveRefreshRate: 200,
};
}, 200);
}
public getBikes(): void {
this.bikes_typeService.getBikesType().subscribe(
(Response: Bikes_type[]) => {
this.bikes_type = Response;
},
(error: HttpErrorResponse) => {
console.log(error);
alert(error.message);
}
);
}
updateCarousel(): void {
// Use a timeout to allow time for the DOM to update after a resize
setTimeout(() => {
this.customOptions = { ...this.customOptions, autoWidth: true };
// console.log(this.customOptions);
}, 20);
}
@HostListener('window:resize', ['$event'])
onResize(event: any): void {
this.updateCarousel();
}
}
I get this errors logs: -> I have two slides, so in my options I have 1 slide for small (0) to medium (768) and two for rest of breakpoints in the responsive option. As you can see in this logs, it seems that even though the window is resized from initial large breakpoint that showed two slides, it still wants to show two slides on the small breakpoints.
Solved this. After a bit of digging it seems that for my slides ( they are gotten from a backend api ), even though their interface uses String for the ids, it somehow was transformed to a Number. A simple slide.id.toString()
in the <ng-template carouselSlide [id]="slide.id.toString()" style="width: 100%">
made it work.
I am using angular 14 with "ngx-owl-carousel-o": "^14.0.1". ( latest ). It seems that the responsiveness of the carousel breaks if the window gets resized, but comes back online if the page is reloaded. For example:
On the initial loading:
then after resize:
and then, if reloaded, it gets the correct width:
I looked into more solutions and none seems to work. I looked into #107 but this doesn't seem to be my problem, as I put this directly on body.
This is my template.html:
and this is my template.ts:
I tried with these solutions I found online but nothing seems to work: