我想在Angular 4中加載動態腳本。腳本本身的名稱稍後將來自Ajax調用。目前要加載我導入Jquery的JavaScript文件到我的Angular項目中。從那以後,我使用getScript加入方法,可以加載該文件,但如果我要調用這樣的功能:在Angular 4中動態加載Javascript腳本
import { Component } from '@angular/core';
import * as jQuery from 'jquery';
declare var $:any;
declare var jquery:any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular 4 with jquery';
url = "currently hardcoded";
getScript42(){
$.getScript(this.url).done(function() {
logger();
});
}
}
我收到以下錯誤: 找不到名稱「記錄」
但在我JavaScript文件的功能定義如下:
$(document).ready(function(){
function logger(){
console.log("it worked");
}
});
如果我不使用它的工作功能。
所以,現在我的問題你們知道一種方法來加載該文件並調用特定的功能嗎?
Hi @Henry Sachs任何html模板引用? – 2017-09-13 15:30:34
你問什麼原因?我有一個onclick方法的按鈕。 –
沒有理由我沒有:)!,我只是問這個: \t 您好任何html模板引用? – 2017-09-13 17:07:41