How to use built in directives in Angular2

Posted By : Priya verma | 30-Dec-2016

Angular2 has a set of built-in directives which offers functionality to your applications.

import { CommonModule } from '@angular/common';

CommonModule contributes many of the common directives that applications need including ngIf and ngFor.

 

a) ngif: If the expression is evaluated to a true the NgIf shows the elements else it removes this element from DOM.

Syntax of *ngIf:        

            <section *ngIf="showSection">

Note:  Don't forget the asterisk (*) in front of ngIf.

 

b) ngFor: ngFor allows us to build data presentation lists and tables in our HTML templates.

 

Syntax of ngFor

            <li *ngFor="let item of list">

 

c)  NgSwitch: Sometimes you need to render different elements depending on a given condition.

When you run into this situation, you could use ngIf several times like this:

 

Syntax of ngSwitch:

<div [ngSwitch]="condition">

                <template *ngSwitchCase="case1">.....</span>

                <template *ngSwitchCase="case2">.....</span>

                <template *ngSwitchCase="case3">.....</span>

                <template *ngSwitchDefault>...</template>

</div>

d) ngClass:  ngClass directive dynamically binds one or more CSS classes to an HTML element. ngClass directive can be a string, an object, or an array. Best choice when we want to add or remove many CSS classes at the same time.

Syntax of ngClass:

            <div [ngClass]="expression">

THANKS

About Author

Author Image
Priya verma

Priya is a bright UI designer, having skills in , HTML, CSS, javascript, bootstrap. Her hobbies are playing outdoor games.

Request for Proposal

Name is required

Comment is required

Sending message..