简体   繁体   English

Angular 5:Js 不适用于 app.component.html

[英]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.这可能有什么问题。

When put into app.component.html放入 app.component.html 时在此处输入图片说明

When put into index.html放入 index.html 时在此处输入图片说明

您必须将 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.

相关问题 Angular app.component.html 未渲染新组件 - Angular app.component.html is not rendering new component Angular 路由显示相同的 app.component.html - Angular routing displaying the same app.component.html Angular 前端只显示app.component.html - Angular Frontend only displaying app.component.html 无法在Plnkr中加载app.component.html - Failed to load app.component.html in Plnkr Angular:我的 app.component.html 没有注入 index.html<app-root> - Angular: my app.component.html is not injecting into index.html <app-root> 我试图在我的角度组件中使用 Chart.js,但它没有显示图表图像,但是当我在 app.component.html 中使用它时,它可以工作 - I am trying to use Chart.js in my angular component, but it is not showing the chart image, but when I use it at app.component.html, it works Angular 6-从app.component.html删除数据并更新视图上的更改 - Angular 6 - Delete data from app.component.html and updating changes on view Angular 2:例外:app.component.html:1:108中的错误是由于:过多的递归 - Angular 2 : EXCEPTION: Error in app.component.html:1:108 caused by: too much recursion 为什么我的文件app.component.html没有在Angular7中显示路由? - Why my file app.component.html is not showing routing in Angular7? "$event.target.value" 在 app.component.html 文件中引发错误。 在 Angular - "$event.target.value" throws error in app.component.html file. in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM