×

Angular 8 ngClass Directive

Angular 8 ngClass Directive

ngClass Directive is a type of attribute directive. Angular provided built-in directive, and it helps in adding or removing CSS classes on an HTML element. The ngClass directive allows us to apply CSS classes dynamically based on expression evaluation.

[ngClass] selector uses the NgClass directive, and ngClass offers three simple ways through which we can update CSS classes in the DOM.

Syntax:

Create an Interface Class and Array

In this step, we have to create an interface class to define the properties types of cars array.

Run the command to create interface class:

ng generate class Cars

Our cars collection has properties name and color, go to src>app>cars.ts and add the below code:

export interface Cars {
name: string;
color: string;
} 

Then, we will generate the cars array in app.component.ts file same as given below:

 Import {Component} from ‘@angular/core’; 
Import {Cars} from ‘./cars’;
@Component({
selector: ‘app-root’,
templateUrl:’./app.component.html’,
styleUrls: [‘./app.component.css’]
})
export class AppComponent {
cars: Cars[]=[
{
“name”:”MG Hector”,
“color”: ’blue’
},
{
“name”:”Ford”,
“color”: ’olive’
},
{
“name”:”Kia”,
“color”: ’orange’
},
{
“name”:”BMW”,
“color”: ’red’
},
{
“name”:”Jaguar”,
“color”: ’Green’
},
{
“name”:”Suzuki”,
“color”: ’purple’
}
];
} 

Joining NgClass Directive on HTML template

In this last step, we will learn to add the CSS class dynamically to the HTML elements using Angular NgClass directive.

 Example:

Go to app.component.html template and then add the ngClass directive as mentioned below.

{{car.name}]

 In the above example, we are representing cars name to apply the dynamic color class, we declared NgClass object expression to apply the class. If the condition is true, it includes the class name on the HTML element if not, then it will discard the class name.

To see the working demo in our browser, run the given command:

ng serve --open

Output:

Joining NgClass Directive on HTML template

Related Topics

Angular 8 Observables

Observables provide support for passing messages between publisher and subscription in our application. The observables can deliver the multiple values of any type like literal, messages, depending on the content. It is an...

6 minutes read.

Dependency Injection in Angular 8

Dependency injection (DI), is an essential application design pattern. Angular 8 has its own DI framework, which used in the design of Angular application to increase efficiency and portability. Dependencies are the services that...

7 minutes read.

Angular CLI Commands

All CLI commands of Angular Angular CLI is a command-line interface which is used to initialize, develop, and maintain Angular applications. We can use these Command on command prompt or consequentially by an associated...

8 minutes read.

Angular 8 App Loading

How an Angular 8 app loaded and started When we create an Angular app and run it by using ng serve command, it looks like the below screenshot. It is a simple...

3 minutes read.

Routing in Angular 8

Routing Angular Router is a powerful JavaScript router is built and maintained by the Angular core team that can install from the package @angular/router. Routing provides a complete routing library with the possibility of multiple...

4 minutes read.

What is AOT and JIT Compiler in Angular

A program written in a high-level language is translated into machine code or a low-level language by a piece of software called a compiler. HTML templates are the primary building...

3 minutes read.

Property binding in Angular 8

Property binding is the primary way to binding data in Angular.  To bind data to a property of any element, we use square braces[ ]. It is also a one-way data-binding...

2 minutes read.

Angular 8 with Bootstrap

How to install Bootstrap for Angular? Run the following Command in Command prompt. npm install–save bootstrap@3= > The @3 is essential! After that, when we use a project created with Angular CLI 6+, we will...

1 minute read.

Angular 8 Directives

Angular 8 Directives: Directives are instructions in the DOM (Document Object Model). It specifies how to place our business logic in Angular. The directive is markers on a DOM element...

3 minutes read.

Installation of Angular 8

Installation Guides to Angular 8 step by Step Angular combines declarative templates, dependency injection and, end to end tooling to solve development challenges. Angular empowers developers to build an application that lives on the...

4 minutes read.

Angular 8 Conclusion

Angular version 8 looks like a more accessible solution entirely focused on the modern technological trends added features. Like Ivy, route configurations use dynamic imports, builder APIs in the CLI, web worker support,...

2 minutes read.

Event Binding in Angular 8

Event binding in Angular 8 Event binding is a data-binding from an element to a component. User actions such as keystrokes, mouse movement, clicks, and touches can be bound to the component property...

1 minute read.

$timeout service in AngularJS

The discipline of web development is expanding quickly. A technology that is released today will inevitably become obsolete in a few months. The webpages were static in the past and...

4 minutes read.

Angular 8 NgStyle Directive

Angular 8 NgStyle Directive The ngStyle attribute is used to change and style the multiple properties of Angular. We can change the value, color, and size, etc. of the component. It is a built-in...

1 minute read.

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive The ng-Switch Directive hides and shows the HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and...

2 minutes read.

Scalability and Validation Forms in Angular 8

Form Validation Validation is an essential part of managing any set of forms. If we are checking for required fields or querying an external API for a username. Angular 8 provides a set of...

1 minute read.

Angular 8 Unit Testing

What is unit testing? Unit testing is a type of software testing where individual components of the software are tested. It is done during the development of any application. A unit may be...

6 minutes read.

Difference between Angular and React

Difference between Angular and React Parameters AngularReactType Angular is a complete framework. React is a JavaScript library, and much older compared...

2 minutes read.

Dynamic components in Angular 8

Dynamic components in Angular 8 The dynamic component is one of the versatile and core concept introduced in Angular, Component template is not fixed. An application needs to load new elements...

4 minutes read.

Angular 8 File Structure

File Structure in Angular 8: A file structure contains the files for one or more projects. A project is the set of files that comprise an application on a shareable...

3 minutes read.