2017-05-04 71 views
0

我有一個有2000行JQuery腳本的模板。最初我用角度版本1獲得它,它在那裏完美的工作,但是當在Angular 4上導入它時似乎沒有在文檔準備好時加載JQuery。jQuery腳本沒有加載Angular v4

在文件的最後,我有:

(function($) { 
    'use strict'; 
    // Initialize layouts and plugins 
    (typeof angular === 'undefined') && $.Pages.init(); 
})(window.jQuery); 

也許一些相關的角版本或類似的東西。還沒搞清楚。

+1

不是100%肯定之後,但我認爲只有角1使用jQuery引擎蓋下,當新版本不那麼它的可能jQuery不再可用,你必須自己提供。 – GillesC

+0

我可以輕鬆地在Angular 2指令上使用JQuery。我認爲有一些方法可以調用符合新版本要求的init方法。 –

回答

0

解決我的問題尋找到角文檔約ngAfterViewInit

import { Component, ElementRef } from '@angular/core'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    title = 'app works!'; 

    constructor(private _elementRef:ElementRef) {}; 

    ngAfterViewInit() { 
    var s = document.createElement("script"); 
    s.type = "text/javascript"; 
    s.src = "../assets/js/pages.js"; 
    this._elementRef.nativeElement.appendChild(s); 
    } 
} 

https://angular.io/docs/ts/latest/api/core/index/AfterViewInit-class.html