Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic styleUrls in angular 2?

Tags:

css

angular

Is it possible to dynamically inject urls to stylesheets into a component?

Something like:

styleUrls: [
  'stylesheet1.css',
  this.additionalUrls
]

or (wishful thinking and note that this is just fake code):

export class MyComponent implements dynamicUrls {

  ngDynamicUrls() {
    this.inject(['anotherStylesheet.css', 'anotherStylesheet2.css']);
  }
}

Because if you're gonna be able to override certain styles from stylesheet1 without having access to it, how are you supposed to do that? My only idea is to have dynamic styleUrls somehow but I don't think this is even possible from what I could find.

Any ideas?

like image 492
Chrillewoodz Avatar asked Apr 10 '16 15:04

Chrillewoodz


People also ask

What is dynamic content in angular?

Every time user clicks the Compile button component takes the template value, compiles new component of it (backed by the RuntimeComponent class with the predefined name property) and renders: Dynamic Angular.

How to add style to Angular component?

There are several ways to add styles to a component: By setting styles or styleUrls metadata. Inline in the template HTML. With CSS imports.

What is host in Angular?

The :host selector in Angular component, plays the role to apply styles on host element. By default, component style works within its component template. But by using :host selector we can apply styles to host element that resides in parent component.

Which functionality does CSS provide in Angular?

Angular applications are styled with standard CSS. That means you can apply everything you know about CSS stylesheets, selectors, rules, and media queries directly to Angular applications. Additionally, Angular can bundle component styles with components, enabling a more modular design than regular stylesheets.


3 Answers

It's possible in some maybe hack way it worked for me. You can manipulate Angular 2 Component decorator, create your own and return Angular's decorator with your properties.

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

    export interface IComponent {
      selector: string;
      template?: string;
      templateUrl?: string;
      styles?: string[];
      styleUrls?: string[];
      directives?: any;
      providers?: any;
      encapsulation?: number;
    }

    export function CustomComponent( properties: IComponent): Function {
      let aditionalStyles: string;

      try {
          aditionalStyles =  require(`path to aditional styles/${ properties.selector }/style/${        properties.selector }.${ GAME }.scss`);
          properties.styles.push(aditionalStyles);
        } catch (err) {
          console.warn(err)
        }
      }

      return Component( properties );
    }

And in your component replace default angular 2 @Component with new one.

import { CustomComponent } from 'path to CustomComponent';

@CustomComponent({
  selector: 'home',
  templateUrl: './template/home.template.html',
  styleUrls: [ './style/home.style.scss']
})
export class ......
like image 101
Virge Avatar answered Oct 18 '22 22:10

Virge


I have found a solution:
1. I have changed the styleurls to styles in the component decorator.
2. I will get my variable.
3. I will use the require command in my decorator.

import { Component } from '@angular/core';
import { environment } from '../environments/environment';
let lang = environment['lang'];

@Component({
   selector: 'app',
   templateUrl: './app.component.html',
   styles: [require('./app.component.css'), require('./app.component.' + lang + '.css')]
  })

  export class AppComponent {

   constructor() { }

  }

In this basic example I have imported the environment variable and changed the css string dynamically.

like image 34
noah lubko Avatar answered Oct 18 '22 20:10

noah lubko


I used to have the same need to dynamically inject urls to stylesheets and eventually ended to initialize a component for each variable css (in my case 3 differents styles) with empty template and use them in my app component with ngIf condition.

Thanks to the use of property "encapsulation: ViewEncapsulation.None", the style of the selected component is then added to the header of the page and enable to get the correct renderer for the whole page.

like image 1
PetitMel_issa Avatar answered Oct 18 '22 22:10

PetitMel_issa