Angular guid

Dynamic style in Angular

Change style when you want

Yurii K
Quick Code
Published in
3 min readMar 21, 2019

--

Angular give us a great ability to create content dynamically. Everybody know about “if”, “switch” or “for” they are our best friend in every Angular project, but Angular have a lot more useful tools for development.

Angular ngStyle helps to create dynamic style. Let’s take a closer look.

We need to create a couple objects with specific color and width. All those stuff we need to create dynamically. We could use “if” statement, but if you have a couple property we need to check a lot of stuff and we will write a lot of “if” statement. Angular have better solution.

Angular style and ngStyle

On the right side you see properties which we need generate dynamically. On the left side you see Angular code. We have a couple options how we can set those properties, but result will be the same.

Result style and ngStyle

Also we can change one of the properties dynamically and html code will rebuilt it.

Change buttons size

This buttons can change height of our elements. That give our users ability to change something and see the results immediately.

User can change height

Also we could do the same with style classes and we can use conditions to decide what style we must use and we could change all those stuff dynamically.

className
Result className

Angular give us another option how we can use class.

ngClass
Result ngClass

Angular give us a lot of opportunities for creation code and styles dynamically. In this short example we covered different options how we could create dynamical styles.

Originally published at tomorrowmeannever.wordpress.com on March 21, 2019.

--

--

Yurii K
Quick Code

Full-stack developer. In my work, I use Angular and C#. In my free time I write articles for my blog.