Ajsrecordfromexercise 1to5
Ajsrecordfromexercise 1to5
ng v
After complete the installation check all node modules are installed or not
Create new component called hello and render hello angular on the page
<div>
<span>{{name}}</span>
</div>
Component.ts
Component.html
What is Component?
For creating user define component we need to use this command
>ng g c test
After execute above we find below files for test component
E:\Angular\hello-world>npm start
1 c . Add an event to the hello component template and when it is clicked, it should change the
courseName.
<p>test works!</p>
<button (click)="onclick()"> Click Me... </button>
<button (click)="message='this message from second button'"> Click Me..
</button>
<h2>{{message}} </h2>
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent {
//public name ="abc";
public message = "";
onclick()
{
this.message="this is my message..."
}
}
2 a . Course Name : Structural Directives
Nglf
Ngswitch
Ngfor
Create a login form with username and password fields. If the user enters the correct credentials, it
should render a "Welcome <>" message otherwise it should render "Invalid Login!!! Please try
again..." message
test.component.html
<div *ngIf="!submitted">
<form>
<label>User Name</label>
<input type="text" #username /><br /><br />
<label for="password">Password</label>
<input type="password" name="password" #password /><br />
</form>
<button (click)="onsubmit(username.value, password.value)">Login</button>
</div>
<div *ngIf="submitted">
<div *ngIf="isAuthenticated; else failureMsg">
<h4>Welcome {{ username }}</h4>
</div>
<ng-template #failureMsg>
<h4>Invalid Login !!! Please try again...</h4>
</ng-template>
<button type="button" (click)="submitted = false">Back</button>
</div>
test.component.ts
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent {
//public name ="abc";
isAuthenticated! : boolean;
submitted = false;
username! : string;
ngFor:
ngFor directive is used to iterate over collection of data
2bCreate a courses array and rendering it in the template using ngFor directive in a list format
.html
<p>--- ngfor ---</p>
<ul>
<li *ngFor="let course of courses; let i = index">
{{i}} - {{ course}}
</li>
</ul>
<ul>
<li *ngFor="let subject of subjects">
{{subject}}
</li>
</ul>
<h1> names ... </h1>
<ul>
<li *ngFor="let name of names">
{{name}}
</li>
</ul>
Output :
.ts
ngSwitch
ngSwitch adds or remove DOM tree when their expression match the switch expression
2c) Display the correct option based on the value passed to ngSwitch directive.
.ts file
@Component({
selector: 'app-dir',
templateUrl: './dir.component.html',
styleUrls: ['./dir.component.css']
})
export class DirComponent {
choice=0;
nextchoice()
{
this.choice++;
}
}
.html
<div [ngSwitch]="choice">
<p *ngSwitchCase="1">{{choice}} First Choice </p>
<p *ngSwitchCase="2">{{choice}} Second Choice </p>
<p *ngSwitchCase="3">{{choice}} Third Choice </p>
<p *ngSwitchDefault>{{choice}} Default Choice </p>
</div>
<button (click)="nextchoice()"> Next Choice </button>
Out put
2d) Create a custom structural directive called 'repeat' which should repeat the
element given a number of times.
repeat.directive.ts
import { Directive, TemplateRef, ViewContainerRef,Input } from
'@angular/core';
@Directive({
selector: '[appRepeat]'
})
export class RepeatDirective {
constructor(private templateRef: TemplateRef<any>, private viewContainer:
ViewContainerRef) { }
@Input() set appRepeat(count: number) {
for (let i = 0; i < count; i++) {
this.viewContainer.createEmbeddedView(this.templateRef);
}
}
}
app.component.html
<h2> repeat directive </h2>
<p *appRepeat="5">hello</p>
Output:
Out put :
3b) Apply multiple css classes to the text using ngClass directive
app.component.html
<div [ngClass]="{bordered: isBordered}">
Border {{ isBordered ? "ON" : "OFF" }}
</div>
app.componenet.ts
export class AppComponent {
title = "ACET";
isactive = "Active";
isBordered = true; }
@Directive({
selector: '[appShowmessage]'
})
export class ShowmessageDirective {
@Input('appShowmessage') message!:string;
constructor(private el: ElementRef,private render:Renderer2 )
{
render.setStyle(el.nativeElement,'cursor','pointer');
}
@HostListener('click') onClick(){
this.el.nativeElement.innerHTML= this.message;
this.render.setStyle(this.el.nativeElement,'color','red');
}
imageurl ='assets/imgs/v.jpeg';
app.component.html
<img [src]="imageurl"/>
Output:
isvalid=true;
}
app.component.html
<button [style.color]="isvalid ? 'blue' : 'red' "> click </button>
<p [style.font-size.px]="isvalid ? 12 : 14"> font sie </p>
Output:
5a. Display the product code in lowercase and product name in uppercase using
built-in pipes
app.component.ts
export class AppComponent {
title = "Product details";
prodcutcode="prod_001";
prodcutname="Laptop";
}
app.component.html
<h3> {{ title | titlecase}} </h3>
<table style="text-align:left">
<tr><th> Product Code </th>
<td> {{ productcode | lowercase }} </td></tr>
<tr><th> Product Name </th>
<td> {{ productname | uppercase }} </td></tr>
</table>
Output:
5b.Passing Parameters to Pipes. Apply built-in pipes with parameters to display
product details
app.component.ts
export class AppComponent {
productCode = 'PROD_P001';
productName = 'Apple MPTT2 MacBook Pro';
productPrice = 217021;
purchaseDate = '1/17/2018';
productTax = '0.1';
productRating = 4.92;
}
app.component.html
<table style="text-align:left">
<tr>
<th> Product Code </th>
<td> {{ productCode | slice:5:9 }} </td>
</tr>
<tr>
<th> Product Name </th>
<td> {{ productName | uppercase }} </td>
</tr>
<tr>
<th> Product Price </th>
<td> {{ productPrice | currency: 'INR':'symbol' }} </td>
</tr>
<tr>
<th> Purchase Date </th>
<td> {{ purchaseDate | date:'fullDate' | lowercase}} </td>
</tr>
<tr>
<th> Product Tax </th>
<td> {{ productTax | percent : '.2' }} </td>
</tr>
<tr>
<th> Product Rating </th>
<td>{{ productRating | number:'1.3-5'}} </td>
</tr>
</table>
OutPut:
5c. Nested Components Basics
Load Course List Component in the root component when a user click on the
view courses list button.
Step 1: create courselist component
E:\Angular\myapp>ng generate component courselist
CREATE src/app/courselist/courselist.component.html (25 bytes)
CREATE src/app/courselist/courselist.component.spec.ts (587 bytes)
CREATE src/app/courselist/courselist.component.ts (218 bytes)
CREATE src/app/courselist/courselist.component.css (0 bytes)
UPDATE src/app/app.module.ts (886 bytes)
Step2: Open courselist.component.ts
import { Component,OnInit } from '@angular/core';
@Component({
selector: 'app-courselist',
templateUrl: './courselist.component.html',
styleUrls: ['./courselist.component.css']
})
export class CourselistComponent {
courses = [{courseid:1,coursename:'nodejs'},
{courseid:2,coursename:'reactjs'}
];
}
Step3: Open courselist.component.html
<table border="1">
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let course of courses">
<td>{{ course.courseid }}</td>
<td>{{ course.coursename }}</td>
</tr>
</tbody>
</table>
Step4:- Open app.component.ts
import { Component,OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
show!:boolean;
}
@Component({
selector: 'app-courselist',
templateUrl: './courselist.component.html',
styleUrls: ['./courselist.component.css']
})
export class CourselistComponent {
courses = [{courseid:1,coursename:'NodeJS'},
{courseid:2,coursename:'ReactJS'},
{courseid:3,coursename:'AngularJS'}
];
course!: any[];
@Input() set cName(name: string) {
this.course = [];
for (var i = 0; i < this.courses.length; i++) {
if (this.courses[i].coursename === name) {
this.course.push(this.courses[i]);
}
}
}
}