1

I'm wondering if is possible to pass a variable to a css class into an external css file in angular 2, like from:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  bgc: string = 'orange'
}

then on the "app.component.css" I would like to pass like

.mydiv{ background: {{bgc}};  }

Is this possible?

1 Answer 1

0

I don't think this would be possible.

If you need a style to change programmatically, I would suggest looking into ngStyle at the link below:

https://angular.io/docs/ts/latest/api/common/index/NgStyle-directive.html

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

4 Comments

Can I apply the ngStyle to the DOM, like <body [ngStyle]="{'font-style': styleExp}">?
Yes, that should be possible.
I think is not possible to add ngStyle to the <body> tag, I ran some test and didn't work
I even try the "ngStyle" with "ViewEncapsulation.None" on the component and didn't work

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.