You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Swiper Element in an Angular component and I want to dynamically update the swiper's direction based on the current language ('ar' for RTL, 'en' for LTR) using a LangService.
The swiper works fine initially, but when the language is changed at runtime, the Swiper layout breaks (e.g. wrong direction, wrong navigation, broken layout).
@Component({selector: 'app-mini-product-swiper',
...
})exportclassMiniProductSwiperComponent{lang=inject(LangService).currentLang;
@Input()swiperClass='';
@Input()swiperBreakpoints={ ... };
@ViewChild('swiperContainer')swiperContainer!: ElementRef;constructor(){effect(()=>{constlang=this.lang();if(lang==='ar'){// 🔄 How to update Swiper to RTL?}else{// 🔄 How to update Swiper to LTR?}});}ngOnInit(): void{register();}ngAfterViewInit(): void{this.assignSwiperParams();}assignSwiperParams(){constswiperElement=this.swiperContainer.nativeElement;constswiperParams: SwiperOptions={modules: [Navigation,Autoplay],slidesPerView: 1,spaceBetween: 16,speed: 500,loop: false,grabCursor: true,navigation: {nextEl: `.${this.swiperClass}-button-next`,prevEl: `.${this.swiperClass}-button-prev`,},autoplay: {delay: 2500,disableOnInteraction: true,},breakpoints: this.swiperBreakpoints,};// now we need to assign all parameters to Swiper elementObject.assign(swiperElement!!,swiperParams);// and now initialize itswiperElement?.initialize();}}
❓ Question
How can I update the Swiper direction ('rtl' or 'ltr') dynamically after the language is changed?
Do I need to destroy and re-initialize the Swiper?
Is there a way to re-assign the direction param and re-render without breaking the layout?
What’s the recommended approach when using in Angular?
💬 Any help or working example would be greatly appreciated. Thank you!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
📋 Problem
I'm using Swiper Element in an Angular component and I want to dynamically update the swiper's direction based on the current language (
'ar'
for RTL,'en'
for LTR) using a LangService.The swiper works fine initially, but when the language is changed at runtime, the Swiper layout breaks (e.g. wrong direction, wrong navigation, broken layout).
📂 Code
swiper.component.html
swiper.component.ts
❓ Question
💬 Any help or working example would be greatly appreciated. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions