Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load css from a CDN in an Angular 2 Component

As the title says, i want to include external css in an Angular 2 component. Here's how i am doing it now:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-auth',
    templateUrl: './auth.component.html',
    styleUrls: [
        'https://fonts.googleapis.com/css?family=Dosis:400,500,600,700',
        'http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css',
        './assets/css/bootstrap.min.css',
        './assets/css/main.css',
        './assets/css/responsive.css',
        './auth.component.css',
    ],
})
export class AuthComponent implements OnInit {

    constructor() { }

    ngOnInit() {
    }

}

The first two URLs don't work. I'm getting an error:

ncaught Error: Cannot find module "./https://fonts.googleapis.com/css?family=Dosis:400,500,600,700"

I can make it work by including it directly into the HTML, but i don't think that's the right way to do it.

Edit: I even tried using encapsulation: ViewEncapsulation.None, that didn't help.

like image 579
Kanav Avatar asked Oct 08 '16 10:10

Kanav


People also ask

What are the different ways to add CSS in angular?

What are the different ways to add CSS in Angular 2? CSS can be added in Angular 2 in 3 ways: Inline - by using the style attribute. Internal - by adding styles property to the @Component decorator and define the styles.

How to dynamically load CSS in Angular application?

Right Click >> go to inspect the element and Select >> network tab and see the changes here. Finally, it’s time to wrap up. Now we can see how the Angular application is dynamically loaded with CSS. I hope that this article is useful to you.

How to upload CSS to Blob Storage in angular?

Now open Angularcss in a container and upload three different style sheets. For your reference, refer to the below image as mentioned, then create a CSS file named blue.css, black.css under the Angular assets folder. Then the upload will be successfully uploaded to the blob storage. Now let’s continue to the next step.

Will chrome finally remove component boundaries in angular?

If Chrome finally removes them, then they won't work in Angular as well (again ViewEncapsulation.Native only). Styles added globally ( index.html) don't consider component boundaries.


2 Answers

You should load only local styles in the array of styleUrls. Therefore, in auth.component.css, import your desired external stylesheets:

@import "https://fonts.googleapis.com/css?family=Dosis:400,500,600,700";
@import "http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css";
like image 104
Alexandru Pufan Avatar answered Nov 09 '22 16:11

Alexandru Pufan


In contrast with templateUrl It seems that styleUrls can only be defined relatively.

A solution to that problem would be to load your absolute external fonts or CSS dependencies from some css code and you could put this piece of css code inside @Component in the inline styles like this:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-auth',
    templateUrl: './auth.component.html',
    styles: ['
        @import "https://fonts.googleapis.com/css?family=Dosis:400,500,600,700";
        @import "http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css";
    '],
    styleUrls: [
        './assets/css/bootstrap.min.css',
        './assets/css/main.css',
        './assets/css/responsive.css',
        './auth.component.css'
    ],
})
export class AuthComponent implements OnInit {

    constructor() { }

    ngOnInit() {
    }

}

If you wish you can transfer your relative dependencies inside styles:[...] and load each of them with a @import. Note: while using the combination styles:[...] + styleUrls:[...] works fine, you can only use templateUrl:'...' or template:'...'

like image 28
Robert Dalla Avatar answered Nov 09 '22 16:11

Robert Dalla