Tôi hy vọng câu hỏi đơn giản.
Tôi muốn chạy một tập lệnh khi Angular2 tương đương với $ document.ready () được kích hoạt. cách tốt nhất để đạt được điều này là gì?
Tôi đã thử đặt tập lệnh vào cuối index.html
, nhưng khi tôi phát hiện ra, điều này không hoạt động! Tôi hiểu rằng nó phải đi trong một số loại khai báo thành phần?
Có thể chạy tải tập lệnh từ .js
tệp không?
EDIT - Mã:
Tôi đã đưa các plugin js và css sau vào ứng dụng của mình (từ chủ đề html Foundry ).
<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
Như đã lưu ý, scripts.js 'khởi tạo' toàn bộ và do đó nó cần được chạy sau khi Angular sẵn sàng. script.js
Làm cho nó hoạt động:
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
}