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 putting the script at the end of index.html
, but as I found out, this doesn't work! I take it that it has to go in some kind of component declaration?
Is it possible to run load the script from a .js
file?
EDIT - Code:
I've got the following js and css plugins injected into my application (from the Foundry html theme).
<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
As noted, the scripts.js 'instantiates' the whole thing, and thus is needed to be run after Angular is ready. script.js
Got it working:
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
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…