【问题标题】:Notify the Directive on changes Angular2通知有关更改 Angular2 的指令
【发布时间】:2016-11-23 15:44:41
【问题描述】:

我有问题,帮忙解决 我有一个带有模板的组件

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

还有指令滑块

@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);


    }

}

但是该指令是在组件中的数据之前触发的 怎么做才能推迟启动指令渲染组件

【问题讨论】:

    标签: typescript angular angular2-directives angular2-components


    【解决方案1】:

    您可以利用组件/指令生命周期挂钩,例如 ngAfterViewChecked

    这是一个示例:

    @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
          });
        }
    }
    

    有关详细信息,请参阅此文档:

    【讨论】:

    • 它对我不起作用我将尝试详细说明我有一个请求数据并获取他们的平局的服务,如果您只是离开指令,滑块需要添加到此数据中,滑块是导致获取这个并开始绘制我理解父组件应该会导致它收到数据并开始绘制的事件,然后必须启动一个脚本指令
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2016-07-07
    • 2017-09-20
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    相关资源
    最近更新 更多