Structural Directives and Event Binding
Structural Directives and Event Binding
*NgFor
Example : 1
Typescript
people: any[] = [
{
"name": "Douglas Pace"
},
{
"name": "Mcleod Mueller"
},
{
"name": "Day Meyers"
},
{
"name": "Aguirre Ellis"
},
{
"name": "Cook Tyson"
}
];
HTML
Example 2 :
Typescript
people: any[] = [
{
"name": "Anil Singh",
"age": 29,
"country": 'IN'
},
{
"name": "Alok Singh",
"age": 33,
"country": 'USA'
},
{
"name": "Raju",
"age": 28,
"country": 'UK'
},
{
"name": "Dilip Singh",
"age": 34,
"country": 'NP'
},
{
"name": "Param Trip",
"age": 32,
"country": 'USA'
}
]
HTML
<ul>
<li *ngFor="let element of people">{{element.name}},
{{element.age}}</li>
</ul>
*NgIf
• If your condition is false [*ngIf='false'] the element the directive is attached to will
be removed from DOM.
Syntax:
Example1
HTML
<div *ngIf="myArr; then tmpl1 else tmpl2"></div>
<ng-template #tmpl1>I'm here</ng-template>
<ng-template #tmpl2>I'm not here</ng-template
ts
myArr = ['him','hers','yours','theirs'];
*ngSwitch
• NgSwitch uses ngSwitchCase keyword to define a set of possible element trees and
• ngSwitchDefault is used to define default value when expression condition does not match
to any element tree defined by ngSwitchCase .
TYPE SCRIPT
inpvalue: number = 2;
HTML
<h4>NgSwitch</h4>
<ul *ngFor="let person of people" [ngSwitch]="person.country">
<li *ngSwitchCase="'UK'" class="a"> {{ person.name }} ({{ person.country }})
</li>
<li *ngSwitchCase="'USA'" class="b"> {{ person.name }} ({{ person.country }})
</li>
<li *ngSwitchCase="'HK'" class="c"> {{ person.name }} ({{ person.country }})
</li>
<li *ngSwitchDefault class="d"> {{ person.name }} ({{ person.country }})
</li>
</ul>
Event Binding
• Event binding allows you to define events that occur in the template (user-
initiated), and communicate to the component class.
• Event Binding will help to build interactive web applications with the flow of data
from component to the element and from element to component - both ends.
• Event binding syntax will have a target event name within parentheses on the left of
an equal sign, and a quoted template statement on the right.
Syntax: (event)
• The $event is a DOM element object with different properties like target and
target.value.
Example 1 :
// THIS DEMO DEMO SHOW THE FILL OF COMBO BOX USING NGFOR AND BUTTON
CLICK EVEN
HTML
<div> Months :
<select>
<option *ngFor = "let i of months">{{i}}</option>
</select>
</div>
<br/>
<button (click)="myClickFunction($event)">
Click Me
</button>
TYPE SCRIPT
isavailable = true;
myClickFunction(event) {
//just added console.log which will display the event details in browser on click of the button.
alert("Button is clicked");
console.log(event);
}
Example 2 :
// THE ABOVE EVENT CALL ON THE DROP DWON CLICK ALSO
HTML
<div> Months :
<select (change) = "changemonths($event)">
<option *ngFor = "let i of months">{{i}}</option>
</select>
</div>
changemonths(event) {
console.log("Changed month from the Dropdown");
console.log(event);
}
Example 3:
HTML
<div> Months :
<select (change) = "changemonths($event.target.value)">
<option *ngFor = "let i of months" value={{i}}>{{i}}</option>
</select>
</div>
<br/>
<button (click)="myClickFunction($event)">
Click Me
</button>
TYPE SCRIPT
changemonths(eve:any) {
alert(eve);