Professional Documents
Culture Documents
Ccs WT Unit 5 Part2
Ccs WT Unit 5 Part2
Example
Step
Following
Demo
"ngFor="let
<h Or
list.
Directive
Ihis
4.18.1.2
ngfor
Now
step Stacky
Open 1: 4:
The
ent.ts Go ngFor type a
Is + A A
click Exccute nWob
theWelcome
user!! Click
Hideto
the
to
Cxample item Good-bye!! show
Click to Develtheopment
is of Click
folder of
prefixed evoba
iterms;">...</li>
structural
to
illustrates code
location by
Hide'
localhost:4200 )
file
dircctive
*. localhost:4200 button using
and The
in how DirectiveDemo A ng
cdit which syntax that serve 55-4
DirectiveDemo
is lo
as the use shows -0
IS
follows
application *ngl'or the
- template
lor directive
ngFor lor
lor cach
Expross
Angular
and
is
displaying
to item
be in
ereated. the X
the collection
list
of
FullStack Web Development 4 - 56 Express and Angular
app.component.ts
import { Component) from'@angular/coro;
@Component({
selector: 'app-root,
templateUrl: /app.component.html,
styleUrls: [Vapp.component.css]
export class AppComponent (
students =|
{roll no:2,name:'Archana'},
{roll no:4,name:'Chinmaya },
{roll no:6,name:'Soumya'},
{roll no:8,name:Swati)
Step 2 : Now open the html file and edit the code as follows -
app.component.html
<h1> List of Students </h1>
<table border=5>
<th>Roll No</th>
<th> Name </th>
<tr *ngFor="let student of students;">
<td>{{student.roll no}}</td>
<td>{{student.name}}</td>
</tr>
</table>
Step 3 : Run the above application at the command prompt by issuing the command
ng serve -o. The web browser will get opened up and the output will be displayed as
follows -
DirectiveDemo X
) localhost:4200
A
List of Students
+ Roll No Name
Archana
Chinmaya
Soumya
8 Swati
@Component({
selector: 'app-root,
templateUrl: ./app.component.html,
styleUrls: [/app.component.css']
})
export class AppComponent{
counter=3;
}
html file and edit the code as follows -
Step 3 : Now open the
app.component.html
</h3>
<h3> ngSwitch Demo
<div [ngSwitch]='counter">
selected</div>
*ngSwitchCase="1"> 1 is selected</div>
<div
*ngSwitchCase="2"> 2 is
<div </div>
*ngSwitchCase="3"> 3 is selected
<div *ngSwitchCase="4"> 4is selected</div>
<div *ngSwitchDefault>Default value</div>
<div
</div>
Stey 4 alyne ayhton at the voand pnpt by tssing the
ng we lhenrh bou will et opened up and the nitpnt will he COmmant
displayet
DrectiveDemo
wlocalhost 1200
ngswitch Demo
3 is selected
Now if we set counter =99 in the app.component.ts file then the output will be
A DirectiveDemo X
localhost:4200
ngSwitch Demo
A
Default value
+
Step 2 : When we want to use the template form, the first thing which we need
import FormModule. Open app.module.ts file and import FormModule as follows -
app.module.ts
import NgModule } from '@angular/core';
import {BrowserModule } from @angular/platform-browser;
'./app-routing.module';
import {AppRoutingModule } from
./app.component!;
import {AppComponent } from
'@angular/forms';
import { FormsModule } from
@NgModule({
declarations: [
AppComponent
),
imports:[
BrowserModule,
FormsModule,
AppRoutingModule
providers: |],
bootstrap: (AppComponent|
}) {}