How to run a jquery function in Angular 2 after every component finish loading

前端 未结 5 1014
感动是毒
感动是毒 2020-11-30 07:02

I have tried all the life cycle hooks but can\'t get to accomplish the needed result. The result that I need is triggering a function that initialize many jquery plugins us

5条回答
  •  忘掉有多难
    2020-11-30 07:14

    One possible solution would be subscribing on zone.onStable or zone.onMicrotaskEmpty

    ngAfterViewInit() {
      this.zone.onStable.first().subscribe(() => {
        debugger;
      });
    }
    

    or

    ngOnInit() {
      this.service.getData().subscribe(() => {
        this.data = data;
        this.zone.onMicrotaskEmpty.first().subscribe((data) => {
          $(someElem).plugin();
        });
      });
    }
    

    See also

    • Angular 2 calling jQuery after rendering elements - after consuming API

提交回复
热议问题