Professional Documents
Culture Documents
MPT PRACTICALS FILE
MPT PRACTICALS FILE
AIM: Setup Environment For Angular Framework By Installing Node.Js, Npm Package Manager
Using Editor Like Visual Code.
Steps For Download NODE JS,NPM , CLI
Step 1: First Open Any Browser And Search Node Js, Then Simply Tap On NODE JS
Step 10: Now Open The Command Prompt And Check The Version On NODE JS And NPM
`
If The Versions Are Display There Then Installation Of NODE JS & NPM Is Completed.
Step 11: Now Install The CLI,
Step 13: Press The “Download For Windows” Button On The Website To Start The Download Of The Visual Studio
Code Application.
Step 14: When The Download Finishes, Then The Visual Studio Code Icon Appears In The Downloads Folder.
Step 15: Click On The Installer Icon To Start The Installation Process Of The Visual Studio Code.
Step 16: After The Installer Opens, It Will Ask You For Accepting The Terms And Conditions Of The Visual Studio
Code. Click On I Accept The Agreement And Then Click The Next Button,
Step 17: Choose The Location Data For Running The Visual Studio Code. It Will Then Ask You For Browsing The
Location. Then Click On Next Button,
Step 18: Then It Will Ask For Beginning The Installing Setup. Click On The Install Button,
Step 19: After Clicking On Install, It Will Take About 1 Minute To Install The Visual Studio Code On Your Device,
Step 20: After The Installation Setup For Visual Studio Code Is Finished, It Will Show A Window Like This Below.
Tick The “Launch Visual Studio Code” Checkbox And Then Click On Finish,
Step 21:After The Previous Step, The Visual Studio Code Window Opens Successfully. Now You Can Create A New
File In The Visual Studio Code Window And Choose A Language Of Yours To Begin Your Programming Journey,
PRACTICAL-2
AIM :Create First Application To Print Hello World Message Using Angular Framework.
Step 1:First Open The Command Prompt, And Write "NG NEW APPLIVATION_NAME".
Step 5:After The Completion The Process, Type "CD APPLICATION_NAME" (CD=Change Directory).
Step 6:After That Type "CODE ." It Open The VS CODE.
<h1>HELLO WORLD</h1>
In title tag
Enter your title
And Run That Code Using "F5" , In Last The Output Is,
PRACTICAL - 3
AIM :Design A Web Page To Utilize Property Binding And Event Binding Concepts Using Button And
Textbox Controls.
Property Binding
First Go In SRC APP APP.COMPONENT.TS And Write
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html', styleUrls: ['./app.component.css'] })
export class AppComponent{
userName:string="GTU";
}
After that go in In SRC APP APP.COMPONENT.HTML and write <input type="text" [value]="userName">
Output:
Event Binding
Output:
PRACTICAL - 4
AIM: Create various components of web page using Attribute Directives.
Output :
PRACTICAL - 5
AIM :Design A Web Page To Display Student Grading System In Tabular Format With Alternate
Color Style Using Ngswitch, Ngstyle Directives.
First Go In SRC APP APP.COMPONENT.TS and Write:
import { Component } from '@angular/core';
@Component({
selector: 'app-practical-five',
templateUrl: './practical-five.component.html', styleUrls: ['./practical-five.component.scss']
})
export class PracticalFiveComponent{ studentGrades :any = [
{name: 'John', grade: 'A'},
{name: 'Jane', grade: 'B'},
{name: 'Mark', grade: 'C'},
{name: 'Mary', grade: 'D'}, {name: 'Peter', grade: 'F'}
];
}
After that go in In SRC APP APP.COMPONENT.HTML and write
<table>
<thead>
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of studentGrades; index as even" [ngClass]="even % 2 === 0 ? 'bg-blue' : 'bgwhite'">
<td>{{ student.name }}</td>
<td [ngSwitch]="student.grade">
<span *ngSwitchCase="'A'" [ngStyle]="{'color': 'yellow'}">{{ student.grade }}</span>
<span *ngSwitchDefault>{{ student.grade }}</span>
</td>
</tr>
</tbody>
</table>
After that go in In SRC APP APP.COMPONENT.CSS and write table {
border-collapse: collapse; width: 100%; margin: 0%;
}
th, td {
text-align: center; padding: 18px; border: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) { background-color: #f2f2f2;
}
tr:hover { background-color: #ddd;
} .bg-blue{ background-color: blue; color: white; }
.bg-white{ background-color: white; color: rgb(113, 41, 41);
}
Output:
PRACTICAL-6
AIM: Design component to perform following tasks
To Add or Remove number of students using textbox and button controls and display it in tabular
structure format.
Give row level remove button option to student table and record should be deleted when click on it.
Practical-six.component.ts:
Practical-six.component.html:
<div class="container">
<div class="row mt-4">
<div class="col-md-12 text-end">
<!--<button class="btnbtn-primary" (click)="addStudent()">
Add Student
</button> -->
</div>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Student Name</th>
<th scope="col">Roll Number</th>
<th scope="col">Address</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of studentList; index as k">
<th scope="row">{{k+1}}</th>
<td>{{item.firstname}}</td>
<td>{{item.roll_no}}</td>
<td>{{item.address}}</td>
<td><a class="pointer" (click)="remove(k)"><img height="20" width="20" src="https://cdn-icons-
png.flaticon.com/512/6861/6861362.png" alt=""></a>
</td>
</tr>
</tbody>
</table>
</div>
Output:
PRACTICAL-7
AIM: Create a component to display a products list from array. The product component should
display a product Id, name, purchase date, price, and image for the product and search using various
pipes.ts: . practical-7.component
practical-7.component.html:
<div class="container">
<div class="row my-4">
<div class="col-md-6">
<input type="text" class="form-control" placeholder="search here..."
[(ngModel)]="searchText" name="searchText">
</div>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">name</th>
<th scope="col">purchase date</th>
<th scope="col">price</th>
<th scope="col">image</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of productList | filter:searchText">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.purchase_date| date:'mediumDate'}}</td>
<td>{{item.price}}</td>
<td><img [src]="item.image" width="100" height="100"></td>
</tr>
</tbody>
</table>
</div>
OUTPUT:
PRACTICAL-8
AIM: Design a student registration page using template driven form approach and utilize different
form and controls level ng validation classes. practical-8.component.ts:
practical-8.component.html:
<div class="container mt-5">
<form #quickAdd="ngForm">
<div class="row">
<div class="col-md-6 mb-3">
<div class="row">
<label class="col-sm-4 col-form-label">Student Name<sup>*</sup></label>
<div class="col-sm-8">
<input type="text" class="form-control" [(ngModel)]="formObj.studentname" name="studentName"
#studentname="ngModel" required placeholder="Student Name">
<div class="text-danger" *ngIf="studentname.errors&&quickAdd.submitted">
<div *ngIf="studentname.errors.required">Student Name is required</div>
</div>
</div>
</div>
</div>
OUTPUT:
PRACTICAL-9
AIM: Design component to enter faculty details like Code, Name, Email, Type, Faculty Status (Active,
Inactive), Subjects Teaching (with option to add multiple subjects dynamically) using reactive form
with various types of validation of form and controls. Practical-9.component.ts:
import { Component, OnInit } from '@angular/core'; import { FormArray, FormBuilder, FormGroup, Validators }
from '@angular/forms';
@Component({
selector: 'app-prectical-nine', templateUrl: './prectical-nine.component.html', styleUrls: ['./prectical-
nine.component.scss']
}) export class PrecticalNineComponent implements OnInit{ facultyForm: any = this.fb.group({ code: ['',
[Validators.required]], name: ['', [Validators.required]], email: ['', [Validators.required]], type: ['',
[Validators.required]], status: ['', [Validators.required]], subjects: this.fb.array([]),
});
submitted = false; constructor(private fb: FormBuilder) { this.facultyForm.subject = this.fb.group({ subjects:
this.fb.array([])
});
} ngOnInit() {
addSubject() { const subject = this.fb.group({ name: ['', Validators.required], code: ['', Validators.required]
});
console.log("subject>", subject) this.subjects.push(subject);
}
get subjects() { return this.facultyForm.get('subjects') as FormArray;
}
removeData(index: number) { this.subjects.removeAt(index);
}
Practical-9.component.html:
<div class="card m-3">
<h5 class="card-header">Angular 10 Reactive Form Validation</h5>
<div class="card-body">
<form [formGroup]="facultyForm" (ngSubmit)="onSubmit()">
<div class="form-row">
<div formArrayName="subjects">
<div *ngFor="let subject of subjects.controls; index as i" class="form-group">
<div [formGroupName]="i">
<label>Name</label>
<input type="text" formControlName="name" class="form-control">
<label>age</label>
<input type="text" formControlName="age" class="form-control">
<button type="button" (click)="removeData(i)">Remove</button>
</div>
</div>
<button type="button" (click)="addSubject()">Add Data</button></div>
OUTPUT:
PRACTICAL-10
AIM: Design a page to implement Add to Cart functionality using decorators, custom properties,
custom events of component communication.
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import
{ AppComponent } from './app.component'; import { ProductListComponent } from './product-list/product-
list.component'; import { ShoppingCartComponent } from './shopping-cart/shopping-cart.component'; import
{FormsModule} from '@angular/forms'; import { CartProductComponent } from './cart-product/cart-
product.component'
@NgModule({
declarations: [
AppComponent,
ProductListComponent,
ShoppingCartComponent,
CartProductComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule{ }
__________________________________________ product-list.component.tsimport { Component,
Input,Output,EventEmitter } from '@angular/core';
@Component({
selector: 'app-product-list', templateUrl: './product-list.component.html', styleUrls: ['./product-list.component.less']
})
export class ProductListComponent {
@Input() products: any[]= Array();
@Output() productAdded = new EventEmitter(); addProductToCart(product:any)
{ this.productAdded.emit(product);
}
}
_____________________________________________ product-list.component.html
<h1>Products List</h1>
<div *ngFor="let product of products">{{product.name}} ---- <button
(click)="addProductToCart(product)">+</button>
<div>{{product.price|currency}}</div></div> ________________________________________________ product-
list.component.css
:host{border: 1px solid #000;}
_______________________________________________ shopping-cart.component.ts
import { Component ,Input,Output,EventEmitter} from '@angular/core';
@Component({
selector: 'app-shopping-cart', templateUrl: './shopping-cart.component.html', styleUrls: ['./shopping-
cart.component.less']
})
export class ShoppingCartComponent {
@Input() products: any[]=Array();
@Output() productRemoved = new EventEmitter();
calcTotal() { return this.products.reduce((acc, prod) =>acc+= prod.num ,0)
}
removeProduct(product:any) { this.productRemoved.emit(product)
}
}
______________________________________________ shopping-cart.component.html
<h1>Shopping Cart ({{calcTotal()}})</h1>
<app-cart-product *ngFor="let product of products"
[product]="product" (productRemoved)="removeProduct($event)"><app-cart-product>
_____________________________________________ shopping-cart.component.css
:host{border: 1px solid #000;}
_____________________________________________ cart-product.component.ts
import { Component,Input,Output,EventEmitter } from '@angular/core';
@Component({
selector: 'app-cart-product', templateUrl: './cart-product.component.html', styleUrls: ['./cart-
product.component.less']
})
export class CartProductComponent {
@Input() product: any;
@Output() productRemoved = new EventEmitter(); modelChanged(product:any) {
if (this.product.num === 0) { this.productRemoved.emit(this.product)
}
}
}
___________________________________________________ cart-product.component.html
<div *ngIf="product">
<div>{{product.name}}</div>
<input type="number" [(ngModel)]="product.num" min="0"
(ngModelChange)="modelChanged($event)"/>
</div>
OUTPUT:
PRACTICAL-11
AIM: Develop page to demonstrate different methods of angular component lifecycle.
@NgModule({
declarations: [
AppComponent,
StudentListComponent,
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule{ }
_______________________________________________ now create new componet using: ng g c student-list
____________________________ student-list.component.html
<!--<p>student-list works!</p> -->
<div #title>
<ng-content></ng-content>
</div>
<ul>
<li *ngFor="let student of students">{{student}}</li>
</ul>
______________________________________________ student-list.component.css
ul li { list-style: none;
}
___________________________________________________ student-list.component.ts
import {
Component,
ContentChild,
ElementRef,
Input,
SimpleChanges,
ViewChild,
} from '@angular/core';
@Component({
selector: 'app-student-list', templateUrl: './student-list.component.html', styleUrls: ['./student-list.component.scss'],
})
export class StudentListComponent {
@Input('studentList') students: string[] = [];
@ViewChild('title') wrapper!:ElementRef;
@ContentChild('titleContent') content!:ElementRef; ngOnChanges(changes: SimpleChanges)
{ console.log('ngOnChanges triggered', changes);
}
ngOnInit() { console.log('ngOnInit from the child component');
}
ngDoCheck() { console.log('ngDoCheck triggered');
}
ngAfterContentInit() { console.log('ngAfterContentInit - title', this.wrapper); console.log('ngAfterContentInit -
titleContent', this.content);
}
ngAfterContentChecked(): void { console.log('ngAfterContentChecked triggered');
}
ngAfterViewInit(): void { console.log('ngAfterViewInit - wrapper', this.wrapper);
}
ngAfterViewChecked(): void { console.log('ngAfterViewChecked triggered');
}
}
_______________________________
run the project using:
ng serve
add students data and see the life cycle change in the console log (right click and inspect element)
OUTPUT:
PRACTICAL-12
AIM: Design an e-commerce product page and product details page that displays product details
when clicking on any particular product.
@Component({
selector: 'app-product-list', templateUrl: './product-list.component.html', styleUrls: ['./product-list.component.less']
})
export class ProductListComponent{ products = products;
}
____________________________________________
product-list.component.html
<h2>Products</h2>
<div *ngFor="let product of products">
<h3>
<a [routerLink]="['/products', product.id]">
{{ product.name }}
</a>
</h3>
<p *ngIf="product.description">
Description: {{ product.description }}
</p>
</div>
________________________________________________ product-details.component.ts
import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import
{ Product, products } from '../products';
@Component({
selector: 'app-product-details', templateUrl: './product-details.component.html', styleUrls: ['./product-
details.component.less']
})
export class ProductDetailsComponent implements OnInit{ product: Product|undefined;
constructor(
private route: ActivatedRoute,
){}
ngOnInit() {
// First get the product id from the current route. constrouteParams = this.route.snapshot.paramMap;
constproductIdFromRoute = Number(routeParams.get('productId'));
OUTPUT:
PRACTICAL-13
AIM: Design a page to display student information using dependency Injection.
@Component({
selector: 'app-student', templateUrl: './student.component.html', styleUrls: ['./student.component.css']
})
export class StudentComponent implements OnInit {
students!: Student[];
constructor(private studentListService: StudentListService) { }
ngOnInit() {
this.getStudentsList();
}
getStudentsList() {
this.students = this.studentListService.getInternsDetails();
}
}
--------------------------------------------------------------------------------------------------------------
student.component.html:
<div>
<table class="table table-stripped table-active">
<thead class="thead-light">
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Admission Number</th>
<th>Course</th>
<th>Year Of Study</th>
<th>Reported On</th>
<th>College</th>
</thead>
<tbody>
<tr *ngFor="let student of students;leti=index">
<td>{{i+1}}</td>
<td>{{student.studentFirstName}}</td>
<td>{{student.studentLastName}}</td>
<td>{{student.studentRegistrationNumber}}</td>
<td>{{student.studentCourse}}</td>
<td>{{student.studentYearOfStudy}}</td>
<td>{{student.reportingDate}}</td>
<td>{{student.college}}</td>
</tr>
</tbody>
</table></div>
------------------------------------------------------------------------------------------------------------- app.component.html:
<app-student></app-student>
<div>
<router-outlet></router-outlet>
</div>
OUTPUT:
PRACTICAL-14
AIM: Develop a page for product listing and search-as-you-type using observables and web APIs from
database.
@Injectable({
providedIn: 'root'
})
export class ProductService{ private apiUrl = 'https://api.example.com/products'; constructor(private http:
HttpClient) {} getProducts(): Observable<any> { return this.http.get<any>(this.apiUrl);
}
searchProducts(query: string): Observable<any> { return this.http.get<any>(`${this.apiUrl}?q=${query}`);
}
}
@Component({
selector: 'app-product-list', templateUrl: './product-list.component.html', styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit{ products$: Observable<any>;
searchQuery: string = ''; constructor(private productService: ProductService) {} ngOnInit() { this.fetchProducts();
}
fetchProducts() { this.products$ = this.productService.getProducts();
}
searchProducts() { this.products$ = this.productService.searchProducts(this.searchQuery);
}
}
Implement the ngOnInit() method to initialize any necessary data. Define a method to handle form submission:
onSubmit(form: NgForm) {
if (form.valid) { const product = form.value; this.productService.addProduct(product).subscribe(() => {
// Reset the form after successful submission form.reset();
// Optional: Fetch the updated product list to reflect the changes in the table this.getProducts();
});
}
}
Step-4 Create the HTML template for the component:
Open the generated product-table.component.html file and add the following code:
<h2>Add Product</h2>
<form (ngSubmit)="onSubmit(productForm)" #productForm="ngForm"><div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" [(ngModel)]="product.name" required></div>
<div>
<label for="price">Price:</label>
<input type="number" id="price" name="price" [(ngModel)]="product.price" required>
</div>
<button type="submit" [disabled]="!productForm.valid">Add</button></form>
<h2>Product List</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of products">
<td>{{ product.id }}</td>
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
</tr>
</tbody>
</table>
Step-5 Update the app component:
Open the app.component.htmlfile and replace its content with the following code:
<app-product-table></app-product-table>
<div>
<router-outlet></router-outlet>
</div>
Step-6 Update the app module:
Open the app.module.ts file and import the necessary modules: import { HttpClientModule } from
'@angular/common/http';
import { FormsModule } from '@angular/forms';
Add HttpClientModule and FormsModule to the imports array:
imports: [
HttpClientModule,
FormsModule
]
Step-7 Run the application:
Start the development server using the command ng serve
Remember to replace the API endpoint (/api/products) in the product.service.ts file with the actual endpoint of web
API. Additionally, you can customize the form fields and the table headers based on your specific requirements.
PRACTICAL-17
AIM: Design a page to implement Multiview component with login, logout functionalities using
different routing options. practical-sevnteen.component.ts:
@Component({ selector:'app-practical-sevnteen',
templateUrl:'./practical-sevnteen.component.html',
styleUrls: ['./practical-sevnteen.component.scss']
}) exportclasspracticalSevnteenComponentimplementsOnInit{
isLoggedIn:boolean=false; username:string='';
password:string=''; constructor() { }
ngOnInit():void {
}
login():void {
// Perform login logic here, e.g., call an authentication serviceif
(this.username==='admin'&&this.password==='password') {
this.isLoggedIn=true;
} }
logout():void {
// Perform logout logic here, e.g., clear session or
tokenthis.isLoggedIn=false;
}
}
practical-sevnteen.component.html:
<divclass="container"><div*ngIf="isLoggedIn;
elseloginView">
<h1>Welcome, User!</h1>
<buttonclass="btnbtn-dange mt-3r"
(click)="logout()">Logout</button>
</div>
<ng-template#loginView>
<!-- Login view -->
<h1>Login</h1>
<divclass="row">
<form(submit)="login()">
<divclass="col-md-6 mb-3">
<inputtype="text"[(ngModel)]="username"name="uu"placeholder="Username"required
class="form-control">
</div>
<divclass="col-md-6 mb-3">
<inputtype="password"class="form-control"
[(ngModel)]="password"name="ii"placeholder="Password"required>
</div>
<divclass="col-md-12">
<buttonclass="btnbtn-primary"type="submit">Login</button>
</div>
</form>
</div>
</ng-template>
</div>
OUTPUT:
PRACTICAL-18
AIM: Develop a page to demonstrate page navigation of product list using routing concepts.
Practical-eighteen.component.ts:
@Component({
selector: 'app-practical-eighteen',
templateUrl: './practical-eighteen.component.html',
styleUrls:['./practical-eighteen.component.scss']
})
export class PracticalEighteenComponent implements OnInit {
constructor(public pt: ProductService) {}
ngOnInit(): void {
}
Practical-eighteen.component.html:
@Component({ selector:'app-practical-eighteen-details',
templateUrl:'./practical-eighteen-details.component.html',
styleUrls: ['./practical-eighteen-details.component.scss']
}) exportclassPracticalEighteenDetailsComponentimplementsOnInit {
productId:any;
product:any;
constructor( privateroute:ActivatedRoute,
privateproductService:ProductService
) { }ngOnInit() { this.route.paramMap.subscribe((params:any) => {
this.productId=+params.get('id');
this.product=this.productService.getProduct(this.productId);
});
}
<divclass="container text-center mt-5">
<h2>{{product.name}}</h2>
<p>{{product.description}}</p>
</div>
OUTPUT:
PRACTICAL-19
AIM: Design a page to load customer and Sales order data using lazy loading technique in angular.
Customer list:
practical-nineteen-one.component.ts:
Sales order:
practical-nineteen-two/practical-nineteen-two.component.ts:
import{ NgModule } from'@angular/core'; import { RouterModule, Routes }
from'@angular/router'; import { PracticalNineteenTwoComponent }
from'./practical-nineteentwo/practical-nineteen-two.component';
constroutes:Routes= [
{ path:'', component:PracticalNineteenTwoComponent }
];
@NgModule({ imports:
[RouterModule.forChild(routes)], exports:
[RouterModule]
})
exportclassSelesorderRoutingModule{ }
OUTPUT:
PRACTICAL-20
AIM: Design a page to implement CORS concept. practical-twenty.component.ts:
practical-twenty.component.html
OUTPUT: