0

I have a component that I would like to apply CSS styles to using an object of selectors to CSS props. https://codesandbox.io/s/happy-goldberg-4740z

Styles to be applied from parent

const styles = {
  a: [
    ['color', 'red']
  ],
  b: [
    ['color', 'blue']
  ]
};

Component to be styled dynamically

@Component({
  selector: 'test',
  template: `
    <div>
      <span class="a">I am A</span>
      <span class="b">I am B</span>
    </div>
  `
})
export class Test implements OnInit {
  @Input() stylesToApply: { [key: string]: string[][] };

  constructor(private _renderer: Renderer2, private _element: ElementRef) {}

  ngOnInit() {
    let styleText = '';
    Object.entries(this.stylesToApply).forEach(([key, value]) => {
        const selector = `:host ${ key }`;
        let propStr = '';
        for (let i = 0; i < value.length; i++) {
            propStr += `${ value[i][0] }: ${ value[i][1] };\n`;
        }

        styleText += `${ selector} { ${ propStr } }\n`;
    });

    const styleElement = this._renderer.createElement('style');
    this._renderer.appendChild(styleElement, this._renderer.createText(styleText));
    this._renderer.appendChild(this._elementRef.nativeElement, styleElement);
  }
}

The result should be a <style> element injected into Test like so but the styles are not actually applied to the elements:

<style>
:host a { color: red; }
:host b { color: blue; }
</style>

1 Answer 1

1

The angular docs have a good starting guide on how to achieve this here

You need have an object that contains property CSSStyleDeclaration. i.e.

myStyle: CSSStyleDeclaration = { width: '100px', minHeight: '200px', backgroundColor: 'red' }

then in your component you could do something along the lines of

<div [style]="myStyle"></div>

this allows you to pass the style directly into the element.

another option at your disposal is to use @HostBinding, but that's probably less ideal for what you are trying to do

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

Comments

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.