Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-initializing after change #40

Open
phantomodm opened this issue Aug 9, 2018 · 2 comments
Open

Re-initializing after change #40

phantomodm opened this issue Aug 9, 2018 · 2 comments

Comments

@phantomodm
Copy link

Hello,

I'm running a situation where I am changing slide sources based on input. Basically i'm filtering the loaded images based on a criteria.. What is command to reinitialize the slider using (afterChange) emitter?

Thanks.

@jjz4689
Copy link

jjz4689 commented Feb 14, 2019

I've also run into the same issue, and I find that the slider shows some odd behaviour when filtering/reordering items the slider.

Tried the (afterChange) event emitter, but it never seems to fire. I've also tried a manual unslick and slick through the code, using a ViewChild for the slider, but that doesn't appear to work.

My workaround for now - if it helps anyone - is to use an *ngIf on the ngx-slick in the component template, and then essentially remove and add the slider back in when I need to change the items in the slider.

Example;

In the template:

<ngx-slick *ngIf="showSlider">...</ngx-slick>

and in my update function:

updateItems(): void {
  // update the items in the list
  // ...

  this.showSlider = false; // property in the component the *ngIf of the slider is bound to
  setTimeout(() => this.showSlider = true, 0);
}

You can obviously initialize the showSlider elsewhere for displaying the slider by default, so I've not included it in my example.

Simply setting the showSlider to false, and then straight to true does nothing, as there has been no digest cycle in between.

The way to get around this, is to use a 0 second setTimeout(...), which will cause a re-evaluation of the bindings, causing the slider to be removed from the DOM, and then placed back in straight away.

It is this placing it back in the DOM which causes the slick to be "new" again, and initialize itself - this time, using the updated list items.

Hope to have a proper solution to this problem; however, in the meantime I hope my example above helps anyone else out who's having this problem.

@phantomodm
Copy link
Author

phantomodm commented Feb 14, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants