NickPiscitelli / Glider.js

A fast, lightweight, dependency free, native scrolling carousel alternative!
https://nickpiscitelli.github.io/Glider.js
MIT License
3.26k stars 296 forks source link

Angular dynamic elements not working #139

Open vdg437 opened 3 years ago

vdg437 commented 3 years ago

I setup my Angular project and I used glider.js in some components with static html which is working perfectly fine.

The view I currently work on has a *ngFor to create the img tags inside the div which glider should display as a carousel.

I tried calling the refresh(true) function inside ngOnInit() and ngAfterViewInit() but it doesnt change anything

As soon as I remove *ngFor and create some elements manually it is working.

You can try it yourself like this:

-- Not working:

<div #glider>
      <div *ngFor="let image of [1,2,3,4], let i = index">Hello {{ i }}</div>
</div>

-- Working

<div #glider>
      <div>Hello 1</div>
      <div>Hello 2</div>
</div>

Could you please help me what I am doing wrong?

If you want I can create a codePen project to clarify the issue

nosto-mikpou commented 3 years ago

I'm not sure whether this would apply to your case but I had similar situation today with VueJS app + Glider. Background for this was to use v-for to loop an array of items which would become slides for Glider. The array content would update by the query results I have in the app.

How I solved this was setting Glider options skipTrack: true and injecting all content to the manually built glider-track wrapper. Then using the Glider's refresh() inside the Vue component's update lifecycle hook. Without the skipTrack the new items would be set outside of the glider-track and thus look broken.

NickPiscitelli commented 3 years ago

I haven't worked with Vue at all, but that approach mentioned seems similar to the one taken by the react-glider module, which is the reason the skipTrack options exists in the first place.

It might be prudent to look to their module on how they solved issues related to component lifecycle management.