【发布时间】:2016-12-01 11:02:07
【问题描述】:
对不起,如果这个问题是重复的。我仍然无法在网络上找到任何合适的解决方案,所以我在这里发布。
我在 Angular 2 中创建一个组件。我有一个外部 js 文件并动态加载它。在外部 js 文件中,我有一个带参数的函数。如何在ngAfterViewInit 中调用该参数。我是 Angular 2 的新手,所以不知道如何在 Angular 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>
请帮我解决这个问题。
【问题讨论】:
标签: javascript angular typescript angular2-components