- Angular 2 - Services
- Angular 2 - Nested Containers
- Angular 2 - Lifecycle Hooks
- Angular 2 - User Input
- Angular 2 - Custom Pipes
- Angular 2 - Transforming Data
- Angular 2 - Handling Events
- Angular 2 - Data Display
- Angular 2 - Third Party Controls
- Angular 2 - Advanced Configuration
- Angular 2 - Dependency Injection
- Angular 2 - CLI
- Angular 2 - Forms
- Angular 2 - Navigation
- Angular 2 - Routing
- Angular 2 - Error Handling
- CRUD Operations Using HTTP
- Angular 2 - Data Binding
- Angular 2 - Metadata
- Angular 2 - Directives
- Angular 2 - Templates
- Angular 2 - Components
- Angular 2 - Architecture
- Angular 2 - Modules
- Angular 2 - Hello World
- Angular 2 - Environment
- Angular 2 - Overview
- Angular 2 - Home
Angular 2 Useful Resources
- Angular 2 - Discussion
- Angular 2 - Useful Resources
- Angular 2 - Quick Guide
- Angular 2 - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Angular 2 - Directives
A directive is a custom HTML element that is used to extend the power of HTML. Angular 2 has the following directives that get called as part of the BrowserModule module.
ngif
ngFor
If you view the app.module.ts file, you will see the following code and the BrowserModule module defined. By defining this module, you will have access to the 2 directives.
import { NgModule } from @angular/core ; import { BrowserModule } from @angular/platform-browser ; import { AppComponent } from ./app.component ; @NgModule ({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
Now let’s look at each directive in detail.
ngIf
The ngif element is used to add elements to the HTML code if it evaluates to true, else it will not add the elements to the HTML code.
Syntax
*ngIf = expression
If the expression evaluates to true then the corresponding gets added, else the elements are not added.
Let’s now take a look at an example of how we can use the *ngif directive.
Step 1 − First add a property to the class named appStatus. This will be of type Boolean. Let’s keep this value as true.
import { Component } from @angular/core ; @Component ({ selector: my-app , templateUrl: app/app.component.html }) export class AppComponent { appTitle: string = Welcome ; appStatus: boolean = true; }
Step 2 − Now in the app.component.html file, add the following code.
<span *ngIf = appStatus >{{appTitle}} Tutorialspoint </span>
In the above code, we now have the *ngIf directive. In the directive we are evaluating the value of the appStatus property. Since the value of the property should evaluate to true, it means the span tag should be displayed in the browser.
Once we add the above code, we will get the following output in the browser.
Output
![ngIf](/angular2/images/ngif.jpg)
ngFor
The ngFor element is used to elements based on the condition of the For loop.
Syntax
*ngFor = let variable of variablepst
The variable is a temporary variable to display the values in the variablepst.
Let’s now take a look at an example of how we can use the *ngFor directive.
Step 1 − First add a property to the class named appList. This will be of the type which can be used to define any type of arrays.
import { Component } from @angular/core ; @Component ({ selector: my-app , templateUrl: app/app.component.html }) export class AppComponent { appTitle: string = Welcome ; appList: any[] = [ { "ID": "1", "Name" : "One" }, { "ID": "2", "Name" : "Two" } ]; }
Hence, we are defining the appList as an array which has 2 elements. Each element has 2 sub properties as ID and Name.
Step 2 − In the app.component.html, define the following code.
<span *ngFor = let lst of appList > <ul> <p>{{lst.ID}}</p> <p>{{lst.Name}}</p> </ul> </span>
In the above code, we are now using the ngFor directive to iterate through the appList array. We then define a pst where each pst item is the ID and name parameter of the array.
Once we add the above code, we will get the following output in the browser.
Output
![ngFor](/angular2/images/ngfor.jpg)