Angular2 equivalent of $document.ready()

前端 未结 6 1014
清歌不尽
清歌不尽 2020-12-01 03:48

Simple question, I hope.

I want to run a script when the Angular2 equivalent of $document.ready() is fired. What is the best way to achieve this?

I\'ve tried

6条回答
  •  借酒劲吻你
    2020-12-01 04:33

    You can fire an event yourself in ngOnInit() of your Angular root component and then listen for this event outside of Angular.

    This is Dart code (I don't know TypeScript) but should't be to hard to translate

    @Component(selector: 'app-element')
    @View(
        templateUrl: 'app_element.html',
    )
    class AppElement implements OnInit {
      ElementRef elementRef;
      AppElement(this.elementRef);
    
      void ngOnInit() {
        DOM.dispatchEvent(elementRef.nativeElement, new CustomEvent('angular-ready'));
      }
    }
    

提交回复
热议问题