1

I have a problem, help solve I have a component with a template

ul(slider)
    li(*ngFor="let car of getRecentCars()")
        car-poster(bcg="{{car.recentBackgroundUrl}}",  image="{{car.indexImage}}")

And the directive slider

@Directive({
    selector: '[slider]'
})
export class sliderDirective{
    private el: HTMLElement;

    constructor(@Inject(ElementRef) elementRef: ElementRef) {
        this.el = elementRef.nativeElement;
        let _this = this;
        setTimeout(function(){
            $(_this.el).slick({
                infinite: false,
                slidesToShow: 1,
                slidesToScroll: 1
            });
        }, 0);


    }

}

But the directive is triggered before the data in the component how to do that it would be possible to postpone the launch directive rendering component

1 Answer 1

1

You could leverage the component / directive lifecycle hooks like ngAfterViewChecked:

Here is a sample:

@Directive({
    selector: '[slider]'
})
export class sliderDirective{
    private el: HTMLElement;

    constructor(@Inject(ElementRef) private elementRef: ElementRef) {
        this.el = elementRef.nativeElement;
    }

    ngAfterViewChecked() {
      $(this.el).slick({
             infinite: false,
             slidesToShow: 1,
             slidesToScroll: 1
      });
    }
}

See this doc for more details:

Sign up to request clarification or add additional context in comments.

1 Comment

it does not work for me I will try to elaborate I have a service that requests a data and to obtain their draws and the slider need to add to this data if you just leave directive, the slider is caused to obtain this and started drawing I Understanding the parent component should cause the event that it has received the data and began drawing, and then have to start up a script directive

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.