Angular2相当于 $document. ready()

希望问题很简单。

我想在触发 Angular2等效的 $document.ready ()时运行一个脚本。实现这一目标的最佳方法是什么?

我试过把脚本放在 index.html的末尾,但是我发现,这样不行!我认为它必须包含在某种组件声明中?

是否可以从 .js文件运行加载脚本?

编辑-代码:

我在应用程序中插入了以下 js 和 css 插件(来自 Foundry html 主题)。

    <link href="css/themify-icons.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
...




<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/flexslider.min.js"></script>
...
<script src="js/scripts.js"></script> //This initiates all the plugins

如前所述,scripts.js“实例化”了整个过程,因此需要在 Angular 准备好之后运行

成功了:

import {Component, AfterViewInit} from 'angular2/core';


@Component({
selector: 'home',
templateUrl: './components/home/home.html'
})
export class HomeCmp implements AfterViewInit {




ngAfterViewInit() {
//Copy in all the js code from the script.js. Typescript will complain but it works just fine
}
118223 次浏览

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

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
}

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

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

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

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