tonym99
tonym99

Reputation: 11

Angular 2 - can I dynamically change the styleUrl in the component?

Angular 2 - can I dynamically change the styleUrl in the component?

I want to alter the CSS for a component based on a query string variable. I can get the variable ok in the ngOnInit() func, but thats where I want to change the CSS based on this value.

Thanks

Upvotes: 0

Views: 878

Answers (1)

Adel
Adel

Reputation: 88

You could use ngClass for dynamic changing.

In your .html file you could do this:

<div [ngClass]="dynamicClass"></div>

In your component.ts file you could set this variable and change it in the onInit() function:

dynamicClass: string = 'licolor';

ngOnInit(){
    this.dynamicClass = 'nextStyle';
}

More about ngClass to find here:

https://angular.io/api/common/NgClass


ngStyle could maybe be a valid solution for you too.

Upvotes: 1

Related Questions