Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the styles appear embedded on inspect element in Angular 2

I downloaded a free HTML theme named dashgum from the internet. I'm implementing it for an Angular2 application using angular-cli. I converted the css files to .scss and pasted the code to the angular application. I then imported the files in the global styles file named styles.scss to apply the styles to the application like this:

@import url('./scss/bootstrap.scss');
@import url('./scss/font-awesome/css/font-awesome.scss');
@import url('./scss/zabuto_calender.scss');
@import url('./scss/gritter/css/jquery.gritter.scss');
@import url('./scss/lineicons/style.scss');
@import url('./scss/style.scss');
@import url('./scss/style-responsive.scss');

The problem that I'm facing during debugging is that all the styles appear as embedded styles in the browser like this (notice the style tag):

enter image description here

I want the style to appear as external styles while inspecting like in the theme. Please notice it in the following screenshot:

enter image description here

These are the default settings in Angular 2 as I made no apparent changes for the styles to appear embedded when inspecting. Is there any known way to change the settings in Angular 2 for the styles to appear as external styles when inspecting? The embedded styles make it harder for me to debug. Any help pointing out towards the solution would be appreciated.

like image 987
UsamaMan Avatar asked Nov 11 '16 16:11

UsamaMan


People also ask

How to assign style property of HTML element in Angular 2?

Angular 2 allows us to assign the style property of HTML element. The syntax of assigning property is - Here, the expression can be anything, either a property of component, or an expression, or a method return value. Example - In the following example, I am assigning “color” property of element based on the radio button selected.

What are the key features of angular that we have not covered?

But another key feature of Angular that we have not covered yet, is the ability to isolate a component style so that it does not interfere with other elements on the page, and that is what we will be covering on part two of this series: Angular :host, :host-context, ::ng-deep - The Complete Guide.

How to implement varying color of the element using angular?

The varying color of the element needs an embedded HTML style, as its not known upfront. If we run into such an use case using Angular, we can implement it using the ngStyle built-in core directive:

How do I add styles to a class using angular?

most styles can be added simply to the HTML directly, for those we can simply add them to the class property, and no special Angular functionality is needed.


2 Answers

My first advice (like official Angular CLI documentation says) is to put your global library inside .angular-cli.json like this:

  ...
  "styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.min.css",
    "../node_modules/font-awesome/css/font-awesome.min.css",
    "styles.scss"
  ],
  ...

Then just run the app with the --extract-css parameter to see in the browser inspector the source files:

ng serve --extract-css

And you will have this:

Inspector element result

like image 177
toioski Avatar answered Oct 04 '22 01:10

toioski


I have learned that the styles imported in the global styles.scss file always appears embedded when inspecting in the browser. If we want the css to appear as external styles, we will have to use it in components.

Edit:

See toioski's answer above.

like image 20
UsamaMan Avatar answered Oct 04 '22 02:10

UsamaMan