Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Attribute Directives in Angular | Mastering Angular Directives and Pipes
Introduction to Angular
course content

Course Content

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
Attribute Directives in Angular

In Angular, attribute directives play a key role in dynamically changing the behavior and appearance of elements without modifying the structure of the DOM. Two of the most commonly used attribute directives are ngClass and ngStyle. These directives provide flexible ways to control element styles and CSS classes based on the component's state.

What is ngClass?

Note
Definition

The ngClass directive allows you to dynamically add or remove CSS classes on an element based on conditions.

This is especially useful when you want to change the appearance of an element in response to data changes β€” for example, highlighting an active item or applying styles based on status.

ngClass supports several input formats:

Object syntax β€” where the keys are class names and the values are boolean conditions. If the condition is true, the class is applied.

html

component

ts

component

copy
123
<div [ngClass]="{'active': isActive, 'inactive': !isActive}"> This element will have the 'active' class if the isActive variable is true. </div>

In this example, classes are added or removed based on the value of the isActive property in the component.

If isActive is true, the active class is added and inactive is removed. If isActive is false, it's the other way around.

Array of strings β€” where each string represents a class name that will be added.

html

component

copy
123
<div [ngClass]="['class1', 'class2']"> This element will have the classes 'class1' and 'class2'. </div>

Here, the classes listed in the array are applied to the element.

Using an array is helpful when you want to dynamically assign multiple classes β€” especially when the number of classes may vary.

Function β€” where a function returns any of the supported formats: object, array, or string.

html

component

ts

component

copy
123
<div [ngClass]="getClass()"> This element will have the classes returned by the getClass() function. </div>

ngClass can also accept a function that returns an object, array, or string depending on the logic you define.

In this case, the getClass() method returns an object with CSS class names as keys and boolean conditions as values.

  • If isError is true, the error-class will be applied;

  • If isSuccess is true, the success-class will be applied;

  • If both are false, no classes will be added.

What is ngStyle?

Note
Definition

The ngStyle directive is used to dynamically apply inline styles to HTML elements. Unlike traditional CSS classes, ngStyle allows you to directly modify an element's styles using component data. This gives you powerful control over visual presentation based on your component's state.

With ngStyle, you can change properties like background color, font, size, positioning, and more β€” all directly from the component.

How Does ngStyle Work?

ngStyle accepts an object where the keys are CSS property names and the values are expressions that are evaluated during rendering.

If an expression returns a value, that value is applied to the corresponding CSS property of the element.

Example With a Style Object

In this example, we dynamically change the text color and font size of the element:

html

component

ts

component

copy
123
<div [ngStyle]="{'color': textColor, 'font-size': fontSize}"> This element will use the color and font size defined by the variables textColor and fontSize. </div>

If textColor is red and fontSize is 20px, those styles will be applied to the element. This approach allows you to easily combine multiple styles in one expression for greater flexibility.

Example Using a Variable and Expression

If you need to apply styles based on logic, you can use expressions directly in the template:

html

component

ts

component

copy
123
<div [ngStyle]="{'background-color': isActive ? 'green' : 'red'}"> This element’s background color depends on the isActive variable. </div>

If isActive is true, the background color will be green; if false, it will be red. Here, a ternary operator is used to make the logic concise and expressive.

Example With a Dynamic Value

Expressions in ngStyle can also be based on methods. You can use component methods to calculate style values dynamically:

html

component

ts

component

copy
123
<div [ngStyle]="getDynamicStyle()"> This element will use the styles returned by the getDynamicStyle() function. </div>

In this example, the getDynamicStyle() method returns a style object based on the current value of isDarkMode.

This is especially useful when styles depend on external conditions like user preferences, time of day, or app themes.

When Should You Use ngClass vs ngStyle?

  • Use ngClass when working with predefined CSS classes. This is ideal when styles are reused in multiple places and you want centralized style control through your stylesheet.

  • Use ngStyle when you need to apply styles dynamically and directly β€” for example, when setting colors, dimensions, or other visual properties that aren't tied to reusable classes.

By using both ngClass and ngStyle, you can easily and efficiently control the appearance and behavior of your elements based on component data β€” all without having to manually update the DOM.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
Attribute Directives in Angular

In Angular, attribute directives play a key role in dynamically changing the behavior and appearance of elements without modifying the structure of the DOM. Two of the most commonly used attribute directives are ngClass and ngStyle. These directives provide flexible ways to control element styles and CSS classes based on the component's state.

What is ngClass?

Note
Definition

The ngClass directive allows you to dynamically add or remove CSS classes on an element based on conditions.

This is especially useful when you want to change the appearance of an element in response to data changes β€” for example, highlighting an active item or applying styles based on status.

ngClass supports several input formats:

Object syntax β€” where the keys are class names and the values are boolean conditions. If the condition is true, the class is applied.

html

component

ts

component

copy
123
<div [ngClass]="{'active': isActive, 'inactive': !isActive}"> This element will have the 'active' class if the isActive variable is true. </div>

In this example, classes are added or removed based on the value of the isActive property in the component.

If isActive is true, the active class is added and inactive is removed. If isActive is false, it's the other way around.

Array of strings β€” where each string represents a class name that will be added.

html

component

copy
123
<div [ngClass]="['class1', 'class2']"> This element will have the classes 'class1' and 'class2'. </div>

Here, the classes listed in the array are applied to the element.

Using an array is helpful when you want to dynamically assign multiple classes β€” especially when the number of classes may vary.

Function β€” where a function returns any of the supported formats: object, array, or string.

html

component

ts

component

copy
123
<div [ngClass]="getClass()"> This element will have the classes returned by the getClass() function. </div>

ngClass can also accept a function that returns an object, array, or string depending on the logic you define.

In this case, the getClass() method returns an object with CSS class names as keys and boolean conditions as values.

  • If isError is true, the error-class will be applied;

  • If isSuccess is true, the success-class will be applied;

  • If both are false, no classes will be added.

What is ngStyle?

Note
Definition

The ngStyle directive is used to dynamically apply inline styles to HTML elements. Unlike traditional CSS classes, ngStyle allows you to directly modify an element's styles using component data. This gives you powerful control over visual presentation based on your component's state.

With ngStyle, you can change properties like background color, font, size, positioning, and more β€” all directly from the component.

How Does ngStyle Work?

ngStyle accepts an object where the keys are CSS property names and the values are expressions that are evaluated during rendering.

If an expression returns a value, that value is applied to the corresponding CSS property of the element.

Example With a Style Object

In this example, we dynamically change the text color and font size of the element:

html

component

ts

component

copy
123
<div [ngStyle]="{'color': textColor, 'font-size': fontSize}"> This element will use the color and font size defined by the variables textColor and fontSize. </div>

If textColor is red and fontSize is 20px, those styles will be applied to the element. This approach allows you to easily combine multiple styles in one expression for greater flexibility.

Example Using a Variable and Expression

If you need to apply styles based on logic, you can use expressions directly in the template:

html

component

ts

component

copy
123
<div [ngStyle]="{'background-color': isActive ? 'green' : 'red'}"> This element’s background color depends on the isActive variable. </div>

If isActive is true, the background color will be green; if false, it will be red. Here, a ternary operator is used to make the logic concise and expressive.

Example With a Dynamic Value

Expressions in ngStyle can also be based on methods. You can use component methods to calculate style values dynamically:

html

component

ts

component

copy
123
<div [ngStyle]="getDynamicStyle()"> This element will use the styles returned by the getDynamicStyle() function. </div>

In this example, the getDynamicStyle() method returns a style object based on the current value of isDarkMode.

This is especially useful when styles depend on external conditions like user preferences, time of day, or app themes.

When Should You Use ngClass vs ngStyle?

  • Use ngClass when working with predefined CSS classes. This is ideal when styles are reused in multiple places and you want centralized style control through your stylesheet.

  • Use ngStyle when you need to apply styles dynamically and directly β€” for example, when setting colors, dimensions, or other visual properties that aren't tied to reusable classes.

By using both ngClass and ngStyle, you can easily and efficiently control the appearance and behavior of your elements based on component data β€” all without having to manually update the DOM.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
We're sorry to hear that something went wrong. What happened?
some-alt