angular2:包括组件第三方JS脚本 [英] angular2: including thirdparty js scripts in component
问题描述
有没有一种方法,包括而不包括它的index.html的Angular2组件内部第三方JS脚本?
Is there a way to include third party JS scripts inside an Angular2 component instead of including it in index.html?
我有一个封装数据表的表的组成部分。这是一个需要在数据表JS / CSS包括唯一的组件。这将是很好,如果我能保持我的index.html干净。组件装饰并让您指定CSS文件。
I have a table component that wraps dataTables. It's the only component that needs the dataTables js/css includes. It would be nice if I could keep my index.html cleaner. The component decorator does let you specify css files.
我想我的移动组件的HTML里面我的脚本标签,但似乎并没有工作。
I tried moving my script tags inside my component html, but that doesn't seem to work.
推荐答案
在组件模板脚本标记将被删除。一种解决方法是在 ngAfterViewInit()
Script tags in component templates are removed. A workaround is to create the script tag dynamically in ngAfterViewInit()
另请参阅 https://github.com/angular/angular/issues/4903
constructor(private elementRef:ElementRef) {};
ngAfterViewInit() {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://somedomain.com/somescript";
this.elementRef.nativeElement.appendChild(s);
}
另请参阅 http://stackoverflow.com/a/9413803/217408
这篇关于angular2:包括组件第三方JS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!