对不起,如果这个问题是重复的家伙。我无法找到任何合适的解决方案,所以我在这里发布。Angular2调用外部JS文件函数里面的组件
我正在角2创建一个组件。我有一个外部js文件并动态加载它。在外部的js文件中,我有一个带参数的函数。如何在ngAfterViewInit
内调用该参数。我是新来的角2,所以不知道怎么称呼在角2打字稿的js函数,我会后我的代码,供大家参考
app.component.ts
import { Component, OnInit, ElementRef,AfterViewInit } from '@angular/core';
declare var $:any;
@Component({
selector: 'app-root',
template: '<div></div>'
})
export class AppComponent implements AfterViewInit {
urlinput;
constructor(elRef: ElementRef){
this.urlinput = elRef.nativeElement.getAttribute('urlinput');
this.loadScript();
}
ngAfterViewInit() {
// need to call the js function here
//tried like this initializeDataTable(this.urlinput) not worked
}
loadScript() {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "app/Message.js";
head.appendChild(script);
}
}
Message.js(外部JS文件)
function initializeDataTable(dTableURL){
$.ajax({
"url": dTableURL,
"success": function(json) {
var tableHeaders;
$.each(json.columns, function(i, val){
tableHeaders += "<th>" + val + "</th>";
});
$("#tableDiv").empty();
$("#tableDiv").append('<table id="displayTable" class="display" cellspacing="0" width="100%"><thead><tr>' + tableHeaders + '</tr></thead></table>');
$('#displayTable').dataTable(json);
},
"dataType": "json"
});
}
的index.html
<app-root urlinput="app/array.txt">Loading...</app-root>
请帮我解决这个问题。
能否请您纠正我的财产打字稿方式代码,我能不能让你 –
遗憾没工作 –
的问题,你给了链接ñ与此相关,它是从两个.ts文件导入函数,而不是从.js导入.ts文件,希望你能理解 –