Sorry guys if this question is duplicate. I couldn't able to find any proper solution still on web so I am posting here.
I am creating a component in angular 2. I am having an external js file and loading it dynamically. In the external js file, I am having a function with parameter. How can I call that parameter inside the ngAfterViewInit. I am new to Angular 2, so don't know how to call the js function in Angular 2 typescript, I will post my code for your reference
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 (External Js File)
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>
Please help me to resolve this issue.