NgStyle Directive
Angular provides a built-in directive, ngStyle
, to modify a component or element's style
attribute. Here's an example:
Notice that binding a directive works the exact same way as component attribute bindings. Here, we're binding an expression, an object literal, to the ngStyle
directive so the directive name must be enclosed in square brackets. ngStyle
accepts an object whose properties and values define that element's style. In this case, we can see that both kebab case and lower camel case can be used when specifying a style property. Also notice that both the html style
attribute and Angular ngStyle
directive are combined when styling the element.
We can remove the style properties out of the template into the component as a property object, which then gets assigned to ngStyle
using property binding. This allows dynamic changes to the values as well as provides the flexibility to add and remove style properties.
Last updated