1

So, i develop web app using Angular 7, one of the component however is using complex styling from old project, so i plan to just paste static HTML, CSS, and JS from static old project to one of the Angular component, how to do it?

i.e, I want to paste following HTML structure in Angular component:

<html>
<head>
   ...
   ...
   <link rel="stylesheets"..
   <link rel="stylesheets"..
   <link rel="stylesheets"..
</head>
<body>
   ...
   ...
   ...
   <script src="...
</body>
</html>

Notice that i need to use CSS and Script on that static page and only contained in single Angular component, i'm aware of declaring global CSS and JS that we need for our project in angular.json, however in this case i dont need global CSS or global JS, only contained on that one component, so, how to do it?

2
  • @Component({ selector: 'my-cmp', templateUrl: 'map.component.html', styleUrls: ['map.component.css', 'yourCustomStyle1.scss',........]}) Commented Jan 14, 2019 at 16:26
  • for the scripts it depends on what they do. IE: you shouldn't use JS/jQuery/etc to modify angular's html since it use it's own. So it's kinda hard to give you a quite good answer for that. Commented Jan 14, 2019 at 16:27

1 Answer 1

1

If you only want certain styles contained in a single Angular component then you can define then with inline styles on your template.

1) Wrap styles with style tag:

@Component({
  template: `
    <style>
    h1 {
      color: blue;
    }
    </style>
    <h1>This is a title.</h1>
    `
})

2) Normal inline styles in the template tags:

@Component({
  template: '<h1 style="color:blue">This is a title.</h1>'
})

You can also include the script tags in your template to import a JS file whenever you're using this component.

3) Alternatively you can import the CSS files by using @import in your CSS file for the component:

@Component({
  template: '<h1>This is a title.</h1>',
  style: '@import url("custom.css");'
})
Sign up to request clarification or add additional context in comments.

6 Comments

it's not possible, there are more than 15 CSS files and JS to be included for just that one single HTML file
if you put the styles like that they will affect every child component of the used component. Just put it in the component's css file so Angular can give them a selector.
@SebOlens what do you mean?
@.xcode if you have that many files then why not using the @import on the css file for your component?
how about JS inside <script> tag? the JS files mostly from third party libraries like jquery etc.
|

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.