0

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. But when I copy all the code and put directly to the index.html it working. What is possible problem with this.

When put into app.component.html enter image description here

When put into index.html enter image description here

2 Answers 2

2

I was facing the same error when I got to know that once angular creates components, it renders dynamic 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.

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.

This worked for me. I hope it will solve your problem too.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to put your html code in app.component.html and your css code in app.component.css and your js code in app.component.ts.

2 Comments

I put Js and css in the asset folder... and declare in angular.json style and script
Please try it by putting in to particular file that i mention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.