Angular2 equivalent of $document.ready()

前端 未结 6 832
清歌不尽
清歌不尽 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:29

    Copying the answer from Chris:

    Got it working:

    import {AfterViewInit} from 'angular2/core';    
    
    export class HomeCmp implements AfterViewInit {    
    
        ngAfterViewInit() {
           //Copy in all the js code from the script.js. Typescript will complain but it works just fine
        }
    
    0 讨论(0)
  • 2020-12-01 04:29

    I went with this solution so I didn't have to include my custom js code within the component other than the jQuery $.getScript function.

    Note: Has a dependency on jQuery. So you will need jQuery and jQuery typings.

    I have found this is a good way to get around custom or vendor js files that do not have typings available that way TypeScript doesn't scream at you when you go to start your app.

    import { Component,AfterViewInit} from '@angular/core'
    
    @Component({
      selector: 'ssContent',
      templateUrl: 'app/content/content.html',
    })
    export class ContentComponent implements AfterViewInit  {
    
      ngAfterViewInit(){
        $.getScript('../js/myjsfile.js');
      }
    }
    

    Update Actually in my scenario the OnInit lifecycle event worked better because it prevented the script from loading after the views were loaded, which was the case with ngAfterViewInit, and that cause the view to show incorrect element positions prior to the script loading.

    ngOnInit() {
        $.getScript('../js/mimity.js');
      }
    
    0 讨论(0)
  • 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'));
      }
    }
    
    0 讨论(0)
  • 2020-12-01 04:33

    In your main.ts file bootstrap after DOMContentLoaded so angular will load when DOM is fully loaded.

    import { enableProdMode } from '@angular/core';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    
    import { AppModule } from './app/app.module';
    import { environment } from './environments/environment';
    
    if (environment.production) {
      enableProdMode();
    }
    
    
    
    document.addEventListener('DOMContentLoaded', () => {
      platformBrowserDynamic().bootstrapModule(AppModule)
      .catch(err => console.log(err));
    });
    
    0 讨论(0)
  • 2020-12-01 04:43

    In order to use jQuery inside Angular only declare the $ as following: declare var $: any;

    0 讨论(0)
  • 2020-12-01 04:48

    the accepted answer is not correct, and it makes no sens to accept it considering the question

    ngAfterViewInit will trigger when the DOM is ready

    whine ngOnInit will trigger when the page component is only starting to be created

    0 讨论(0)
提交回复
热议问题