vdanyliv / ngx-tiny-slider

Angular wrapper for tiny-slider
https://npmjs.com/package/ngx-tiny-slider
MIT License
15 stars 18 forks source link

Dynamic items through ngFor #13

Closed housebox closed 4 years ago

housebox commented 5 years ago

Slider not initialized when items listed through ngFor. Console prints warn like: No items found in

vdanyliv commented 5 years ago

Hi @housebox, you should use lazy load option domReady

vdanyliv commented 5 years ago

Example:

template:

<div class="item" #slideList *ngFor="let item of list | async;">
 <a href="{{item.url}}" target="_blank"><img src="{{item.img}}"></a>
</div>

component:

@ViewChildren('slideList') slideList: QueryList<any>;

ngAfterViewInit() {
    this.slidesReadyRender();
}

private slidesReadyRender() {
    this.slideList.changes.subscribe(() => this.ngxSlider.domReady.next());
}
vdanyliv commented 5 years ago

@housebox can I close this topic?

housebox commented 5 years ago

Your solution was'not working on my side, the problem is with this.ngxSlider.domReady(), i defined ngxSlider as ViewChild, & domReady() triggered, but nothing happens, still not initialized :(

vdanyliv commented 5 years ago

@housebox can you provide code sample?

lugipfupf commented 5 years ago

You need to set the waiteForDom option to true.

This worked for me:

@ViewChildren('carouselItemList')
private carouselItemList:QueryList<any>;

@ViewChild('ngxSlider')
private ngxSlider:ElementRef<NgxTinySliderComponent>;

public tinySliderSettings: NgxTinySliderSettingsInterface = {
        waiteForDom: true,    // note the typo
        ...
};

ngAfterViewInit(): void {
    this.carouselItemList.changes.subscribe(() => this.ngxSlider.domReady.next());
}

This works even though I get the error message "domReady does not exist on type 'ElementRef<NgxTinySliderComponent>'".

vdanyliv commented 5 years ago

@lugipfupf Will add definition in next release

seanmavley commented 4 years ago

@lugipfupf Following your example, doesn't seem to work. Here's my entire code sample:

This is template

  <ngx-tiny-slider [config]="tinySliderConfig" #ngxSlider>
    <ng-container class="items">
      <div class="item" *ngFor="let cardDetail of cardList" #slideList>
        <img [src]="cardDetail.imageUrl"/>
      </div>
    </ng-container>
  </ngx-tiny-slider>

Then in component, I have this

export class CarouselComponent implements OnInit, AfterViewInit {
  @ViewChildren('slideList') slideList: QueryList<any>;

  @ViewChild('ngxSlider')
  private ngxSlider: ElementRef<NgxTinySliderComponent>;

  constructor() {}

  ngAfterViewInit() {
    this.slideList.changes.subscribe(() => this.ngxSlider.nativeElement.domReady.next());
  }

  ngOnInit() {
    this.tinySliderConfig = {
      waiteForDom: true,
      .....
   }
}

When I run this, no errors anywhere, either in console in browser or angular cli console. And the slider doesn't work too.

I've tried the above using this.ngxSlider.nativeElement.domReady.next() and also this.ngxSlider.domReady.next(). None of them work.

@housebox Did you get yours to work?

vdanyliv commented 4 years ago

@seanmavley I will prepare sandbox with working example this week

seanmavley commented 4 years ago

@vdanyliv Actually I got it working as at yesterday.

Same setup I have above. Only change I did was this

  ngAfterViewInit() {
    this.ngxSlider.domReady.next()
  }

Thanks. A sandbox will be great. Looking forward to it.

vdanyliv commented 4 years ago

@housebox @seanmavley @lugipfupf here is the example with dom-ready event https://codesandbox.io/s/ngx-tiny-slider-wait-for-dom-gvvqh

mksmanjit commented 4 years ago

@vdanyliv I used your the code which is there in the sandbox, not getting it properly why you have slider and sliderLazy and tinySliderConfig and tinySliderConfigLazy and you html is not referring to the lazy one and the content is still static in your html.

I am new to this library can you please create one simple example with having *ngFor in your html, so that new user like me can understand it properly.

vdanyliv commented 4 years ago

@mksmanjit don't know why, but template is not up to date.

In any case, you can find info in repo

https://github.com/vdanyliv/ngx-tiny-slider/blob/develop/src/app/app.component.html

UPD: sandbox updated

mksmanjit commented 4 years ago

@vdanyliv Thanks a lot it worked