[英]Angular 5: Js not working on app.component.html
I want to convert the html template that I buy to angular 5. The problem is when I put the code in the app.component.html the code not working properly the css is work but the js is not working.我想将我购买的 html 模板转换为 angular 5。问题是当我将代码放入 app.component.html 时,代码无法正常工作,css 工作正常,但 js 不工作。 But when I copy all the code and put directly to the index.html it working.但是当我复制所有代码并直接放入 index.html 时,它就可以工作了。 What is possible problem with this.这可能有什么问题。
您必须将 html 代码放在app.component.html
,将 css 代码放在app.component.css
,将 js 代码放在app.component.ts
。
I was facing the same error when I got to know that once angular creates components, it renders dynamic JS.当我知道一旦 angular 创建组件,它就会呈现动态 JS 时,我遇到了同样的错误。 As I too was not likely to split all the JS files, Convert and place them seperately in app.component.ts
of every component.由于我也不太可能拆分所有 JS 文件,因此将它们转换并单独app.component.ts
在每个组件的app.component.ts
中。
Here is an easy approach to achieve that.这是实现这一目标的简单方法。
export class AppComponent {
title = 'angapp';
constructor() {
this.loadScripts();
}
loadScripts() {
// This array contains all the files/CDNs
const dynamicScripts = [
'assets/js/jquery.min.js',
//Load all your script files here'
];
for (let i = 0; i < dynamicScripts.length; i++) {
const node = document.createElement('script');
node.src = dynamicScripts[i];
node.type = 'text/javascript';
node.async = false;
document.getElementsByTagName('head')[0].appendChild(node);
} } }
Replace your class AppComponent
with this code in app.component.ts
and place all your script files in dynamicScripts
array in the same way as example is provided.将您的class AppComponent
替换为class AppComponent
中的此代码, app.component.ts
您的所有脚本文件以与提供的示例相同的方式放置在dynamicScripts
数组中。
This worked for me.这对我有用。 I hope it will solve your problem too.我希望它也能解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.