Mpt Code
Mpt Code
app.component.html file:
<div class='panel panel-primary'>
<div class='panel-heading'>
<h1>{{title}}</h1>
</div> <br /> <br />
<div class='panel-body'>
<div class='table-responsive'> <table
border="1">
<thead> <tr>
<th>Student Name</th>
<th>Maths</th><th>Science</
th><th>English</th
<th>Total</th> <th>Grade</th>
</tr> </thead> <tbody> <tr *ngFor="let
student of students; let i = index" [ngStyle]="{
'background-color': i
% 2 === 0 ? 'coral' : 'lightgray' }">
<td>{{ student.name }}</td> <td>{{
student.maths
}}</td>
<td>{{ student.science }}</td> <td>{{
student.english
}}</td>
<td>{{ student.total }}</td> <td
[ngSwitch]="student.grade"> <span
*ngSwitchCase="'A+'">A+</span> <span
*ngSwitchCase="'A'">A</span> <span
*ngSwitchCase="'B'">B</span> <span
*ngSwitchCase="'C'">C</span> <span
*ngSwitchDefault>No Grade</span> </td>
</tr>
</tbody> </table> </div> </div> </div>
app.component.ts file:
import { Component } from '@angular/core';
@Component({
selector: 'app-root', templateUrl:
'./app.component.html', styleUrls:
['./app.component.css'] }) export class
AppComponent { title: string ="Student Grading
System"; students = [ { name: 'John', maths: 90,
science: 85, english: 92, total: 267, grade: 'A+' },
{ name: 'Jane', maths: 80, science: 75, english:
85, total: 240, grade: 'A' }, { name: 'Tom',
maths: 70, science: 80, english: 75, total: 225,
grade: 'B' }, name: 'Lisa', maths: 65, science: 70,
english: 80, total: 215, grade: 'C' } ]; }
app.module.ts file:
import { NgModule } from '@angular/core';
import { BrowserModule } from
'@angular/platform- browser';
import { AppComponent } from
'./app.component'; @NgModule({ declarations: [
AppComponent, ], imports: [ BrowserModule ],
providers: [], bootstrap: [AppComponent]
}) export class AppModule { }
Prac 6:
student-component.component.html file:
<div class="solid"> <div class='panel panel-
primary'>
<div class='panel-heading'>
<h3>{{title}}</h3> </div>
<div class='panel-body'> <div> <input
type="number" [(ngModel)]="studentCount"
min="0" value="0"> <br />
<br /> <button (click)="updateTable()">
Update Table</button> </div> <br /> <table>
<thead>
<tr> <th>First Name</th>
<th>Last Name</th> <th>Age</th>
<th>Action</th>
</tr> </thead> <tbody> <tr *ngFor="let
student of students; let i=index;"> <td><input
type="text" [(ngModel)]="student.fname"
name="fname{{i}}"></td>
<td><input type="text"
[(ngModel)]="student.lname"
name="lname{{i}}"></td> <td><input
type="number" [(ngModel)]="student.age"
name="age{{i}}" min="0"></td> <td><button
(click)="removeStudent(i)">Remove</button></
td> </tr>
</tbody> </table> </div> </div> </div>
student-component.component.ts file:
import { Component, OnInit } from
'@angular/core'; @Component({ selector: 'app-
student-component', templateUrl: './student-
component.component.html', styleUrls:
['./student-component.component.css'] }) export
class StudentComponentComponent implements
OnInit { title: string ="Students Record" ;
studentCount: number = 0; students: Student[]
= []; ngOnInit() { console.log("init")
this.students.push(new
Student("Shweta","Patel", 21));
this.students.push(new
Student("Neha","Patel", 20));
this.students.push(new Student("Ami","Patel",
22)); this.studentCount = this.students.length; }
updateTable()
{ const currentCount = this.students.length; if
(this.studentCount > currentCount) { for (let i =
currentCount; i < this.studentCount; i++)
{ this.students.push(new Student('', '', 0)); } }
else if (this.studentCount < currentCount)
{ this.students.splice(this.studentCount); } }
removeStudent(index: number)
{ this.students.splice(index, 1); } } class Student
{ constructor(public fname: string, public lname:
string, public age: number) { } }
border: 1px solid #ddd; padding: 8px; text-align:
left; } th
{ background-color: #f2f2f2; }
input[type="number"], input[type="text"]
{ width: 100%; } .center-screen { display: flex;
justify-content: center; align-items: center; text-
align: center; } div.solid { border-style: solid;
padding: 15px;}
app.component.html file:
<app-student-component>
</app-student-component>
app.component.ts file:
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl:
'./app.component.html',
styleUrls: ['./app.component.css'] }) export class
AppComponent { }
app.module.ts file:
import { NgModule } from '@angular/core';
import { BrowserModule } from
'@angular/platform-browser'; import
{ AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { StudentComponentComponent } from
'./student- component/student-
component.component';
@NgModule({ declarations: [ AppComponent,
StudentComponentComponent ],
providers: [],
bootstrap:
[AppComponent]}) export
class AppModule { }
Prac 7:
filter.pipe.ts file:
import { Pipe, PipeTransform } from
'@angular/core'; @Pipe({ name: 'filter' })
export class FilterPipe implements PipeTransform
{ transform(items: any[], searchText: string):
any[] { if (!items) { return []; } if (!searchText) {
return items; } searchText =
searchText.toLowerCase(); return
items.filter(item => { return
item.name.toLowerCase().includes(searchText); }
); } }
filter.pipe.ts file
: import { Pipe, PipeTransform } from
'@angular/core'; @Pipe({ name: 'filter' })
export class FilterPipe implements PipeTransform
{ transform(items: any[], searchText: string):
any[] { if (!items) { return []; } if (!searchText)
{ return items; } searchText =
searchText.toLowerCase(); return
items.filter(item => { return
item.name.toLowerCase().includes(searchText); }
); } }
.center-screen { display: flex; justify-content:
center; align-items: center; text-align: center; }
div.solid { border- style: solid; padding: 15px; }
product-list.component.ts file:
import { Component } from '@angular/core';
@Component({
selector: 'app-product-list', templateUrl:
'./product- list.component.html', styleUrls:
['./product- list.component.css'] })
export class ProductListComponent {
searchText: string
= ''; title: string ="Product List" ; products: any[]
= { id: 1, name: 'Cococola Classic',
purchaseDate: new Date('2022-01-01'), price:
10.99,
imageUrl: 'https://greendroprecycling.com/wp-
content/uploads/2017/04/GreenDrop_Station_Alu
minum
_Can_2-150x150.jpg' },
{ id: 2, name: 'Stainless steel water bottle',
purchaseDate: new Date('2022-02-01'), price:
20.99, imageUrl:
'https://thumbs.dreamstime.com/z/metal- water-
bottle-reusable-stainless-steel-flask-metal-water-
bottle-reusable-stainless-steel-eco-flask-mockup-
empty- aluminum-187861207.jpg' },
{ id: 3, name: 'Perfume bottles vector.',
purchaseDate: new Date('2022-03-01'), price:
30.99, imageUrl:
'https://cdn.w600.comps.canstockphoto.com/perf
ume- bottles-vector-product-clipart-
vector_csp56261954.jpg' },
{ id: 4, name: 'Men Sports Shooes Nike',
purchaseDate: new Date('2022-04-01'), price:
40.99, imageUrl: 'https://images.squarespace-
cdn.com/content/v1/5911f31c725e251d002da9ac
/16132 10424136-
AS3MY547OBB5Y3GSQ359/
Product+Photography?for mat=1000w' }
Product-list.component.html file:
<div class="solid">
<div class='panel panel-primary'>
<div class='panel-heading'>
<h3>{{title}}</h3> </div> <br />
app.module.ts file:
import { NgModule } from '@angular/core';
import { BrowserModule } from
'@angular/platform- browser';
import { AppComponent } from
'./app.component';
import { ProductComponent } from
'./product/product.component';
import { ProductListComponent } from
'./product- list/product-list.component';
@NgModule({ declarations: [ AppComponent,
ProductComponent, ProductListComponent, ],
imports: [ BrowserModule ], providers: [],
bootstrap: [AppComponent] }) export class
AppModule { }
Prac 11:
lifecycle-child.component.ts
import { Component, OnInit, Input, Output,
EventEmitter, OnChanges, DoCheck,
AfterViewInit, OnDestroy }
from '@angular/core';
@Component({ selector: 'app-lifecycle-child',
templateUrl: './lifecycle-child.component.html',
styleUrls: ['./lifecycle-child.component.css'] })
export class LifecycleChildComponent
implements OnInit, OnChanges, DoCheck,
AfterViewInit, OnDestroy
{ @Input() childProp: string;
@Output() childEvent = new
EventEmitter<string>(); showNgDoCheck =
false; constructor() { }
{ this.log('ngOnDestroy');
this.childEvent.emit('Child Component
ngOnDestroy Triggered'); } triggerEvent()
{ this.childEvent.emit('Child Event Triggered'); }
private log(hook: string)
{ console.log(`LifecycleChildComponent $
{hook}`); }}
lifecycle-child.component.html
<p >Child Component Property: </p> <p
[innerHTML]="childProp"></p>
<button (click)="triggerEvent()">Trigger
Event</button>
lifecycle-child.component.ts
import { Component, ChangeDetectorRef, OnInit,
OnChanges, DoCheck, AfterViewInit, OnDestroy }
from '@angular/core';
@Component({ selector: 'app-lifecycle-demo',
templateUrl: './lifecycle-demo.component.html',
styleUrls: ['./lifecycle-demo.component.css'] })
export class LifecycleDemoComponent
implements OnInit, OnChanges, DoCheck,
AfterViewInit, OnDestroy
{ showChild = false; parentProp = 'Initial Parent
Value'; childText = 'Child Component'; message
= ''; constructor(private cdr: ChangeDetectorRef)
{}
ngOnInit() { this.log('ngOnInit');
} ngOnChanges() { this.log('ngOnChanges'); }
ngDoCheck() { this.log('ngDoCheck'); }
ngAfterViewInit() { this.log('ngAfterViewInit'); }
app.component.ts
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl:
'./app.component.html',
styleUrls: ['./app.component.css'] }) export class
AppComponent { title: string ="Angular
Component Lifecycle Demo" ; }
app.module.ts
import { NgModule } from '@angular/core';
import {
BrowserModule } from
'@angular/platform-
browser';
import { AppComponent } from
'./app.component';
import { LifecycleDemoComponent } from
'./lifecycle- demo/lifecycle-demo.component';
import { LifecycleChildComponent } from
'./lifecycle- child/lifecycle-child.component';
@NgModule({
declarations: [ AppComponent,
LifecycleDemoComponent,
LifecycleChildComponent ], imports:
[ BrowserModule ]
, providers: [], bootstrap: [AppComponent]
}) export class AppModule { }
Prac 12:
product-details.component.html
<div class="card">
<div class="card-body"> <br /> <br /> <table>
<thead>
<tr> <th>Image</th> <th>Name</th>
<th>Description</th> <th>Price</th>
</tr> </thead> <tbody> <tr> <td>
<img class="product-image"
[src]="product.image" height="100"
width="100"></td>
<td><h5 class="card-title">{{ product.name
}}</h5></td> <td><p class="card-
text">{{ product.description }}</p></td>
<td><p class="card-text">{{ product.price |
currency
}}</p></td> </tr> </tbody> </table>
</div></div>
product-details.component.ts
import { Component, OnInit
} from '@angular/core';
import { ActivatedRoute } from
'@angular/router';
import { ProductService } from
'../product.service'; @Component({ selector:
'app-product-detail', templateUrl:
'./product-detail.component.html', styleUrls:
['./product- detail.component.css'] })
export class ProductDetailComponent
implements OnInit { product = { id: '', name: '',
description: '', price: 0, image: '' };
constructor( private route:
ActivatedRoute, private productService:
ProductService
) { } ngOnInit()
{ console.log("Product Details ng Onit") const id
= this.route.snapshot.paramMap.get('id');
this.product =
this.productService.getProductById(id); }}
product-list.component.ts
import { Component, OnInit
} from '@angular/core';
import { ProductService } from
'../product.service'; import { Product } from
'../product.interface';
@Component({ selector: 'app-product-list',
templateUrl: './product-list.component.html',
styleUrls: ['./product- list.component.css'] })
export class ProductListComponent implements
OnInit { products = []; constructor(private
productService: ProductService) { }
ngOnInit() { this.products =
this.productService.getProducts();
console.log("ngOnInit this.products === ",
this.products);
}}
app.component.html
<div class='panel panel-primary'>
<div class='panel-heading'>
<h3>{{title}}</h3>
</div> <div class='panel-body'> <router-
outlet>
</router-outlet></div></div>
app.component.ts
import { Component } from
'@angular/core';
@Component({ selector: 'app-root',
templateUrl: './app.component.html', styleUrls:
['./app.component.css'] })
export class AppComponent { title: string
="Product Information" ; movies: Movie[] =[
{title:'Zootopia',director:'Byron Howard, Rich
Moore',cast:'Idris Elba, Ginnifer Goodwin, Jason
Bateman',releaseDate:'March 4, 2016'}
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 { ProductDetailComponent } from
'./product- detail/product-detail.component';
import { RouterModule }
import { AppRoutingModule } from
'./app.routing.module';
@NgModule({ declarations: [ AppComponent,
ProductListComponent, ProductDetailComponent,
], imports: [ BrowserModule, RouterModule,
AppRoutingModule ], providers: [], bootstrap:
[AppComponent] })
export class AppModule { }
.products.find(product => product.id === id); }
} constructor() { }
getProducts()
{ console.log("this.products === ",
this.products); return this.products; }
getProductById(id: string)
{ return this.products.find(product => product.id
=== id);
}}
Prac 13:
student-list.component.html
<h3>Student List</h3> <table>
<thead> <tr> <th>Name</th> <th>Age</th>
<th>Class</th> </tr> </thead> <tbody>
<tr *ngFor="let student of students">
<td>{{ student.name }}</td>
<td>{{ student.age }}</td>
<td>{{ student.class }}</td>
</tr> </tbody> </table>
student-list.component.ts
import { Component, OnInit
} from '@angular/core';
import { StudentService } from
'../student.service'; @Component({
selector: 'app-student-list', templateUrl:
'./student- list.component.html', styleUrls:
['./student- list.component.css'] })
export class StudentListComponent implements
OnInit { students = [];
constructor(private studentService:
StudentService) { } ngOnInit() { this.students =
this.studentService.getStudents(); } }
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from
'@angular/platform- browser';
import { AppComponent } from
'./app.component';
import { StudentListComponent } from
'./student- list/student-list.component';
@NgModule({ declarations: [ AppComponent,
StudentListComponent ],
imports: [ BrowserModule ], providers: [],
bootstrap: [AppComponent]
student.service.ts
import { Injectable }
from '@angular/core';
@Injectable({ providedIn:
'root' }) export class
StudentService
{ students = [ { name: 'John Doe', age: 20, class:
'1A' }, {
name: 'Jane Smith', age: 19, class: '2B' }, {
name: 'Bob
Johnson', age: 21, class: '3C'
} ]; constructor() { }
getStudents()
return this.students; } }
Prac 14:
Package.json
{ "start": "node index.js", "test": "echo \"Error:
no test specified\" && exit 1" }, "author":
"Saurabh Pathak",
"license": "ISC", "dependencies": { "cors":
"^2.8.5",
"express": "^4.17.1", "lodash": "^4.17.21",
"moment":
"^2.29.4", "mysql2": "^3.2.0" } }
product.controller.js
const moment =
require('moment'); const
{ Constants:
{ RESPONSE_STATUS_CODE: { BAD_REQUEST,
SUCCESS, INTERNAL_SERVER_ERORR },
RESPONSE_MESSAGE: {
BAD_REQUEST_MESSAGE,
INTERNAL_SERVER_MESSAGE }, } } =
require("../utils/constant")
const productServices =
require("../services/product.services")
exports.searchProduct = (req, res, next) =>
{ try { let { keyword } = req.query; let error =
[]; if(error.length > 0) { res.body =
{ statusCode: BAD_REQUEST,
message: BAD_REQUEST_MESSAGE, error: error
} next(); } const products =
productServices.searchProduct(keyword);
console.log("products ==== ", products);
res.body = { statusCode: SUCCESS, message:
`product list retrieved`, data: products
} next(); }
catch(err) { console.log("err", err); res.body = {
statusCode:
INTERNAL_SERVER_ERORR, message:
INTERNAL_SERVER_MESSAGE, error: err } next();
}}
Product-listing.component.html
<div class="solid">
<div class='panel panel-primary'> <div
class='panel- heading'> <h3>{{title}}</h3>
</div> <br /> <div class='panel-body'>
<div class='table-responsive'>
<input type="text" placeholder="Search
products" (keyup)="onSearch($event)">
<br /> <table> <thead> <tr>
<th>ID</th> <th>Name</th>
<th>Description</th> <th>Price</th>
<th>Image</th> </tr> </thead>
<tbody> <tr *ngFor="let product of products">
<td>{{ product.id }}</td>
<td>{{ product.name | uppercase}}</td>
<td>{{ product.description }}</td>
<td>{{ product.price | currency }}</td>
<td><img [src]="product.image" height="100"
width="100"></td>
</tr> </tbody> </table> </div>
</div> </div>
</div>
product-listing.component.css
table { border-collapse: collapse; width: 100%; }
th,
td { border: 1px solid #ddd; padding: 8px; text-
align: left;
} th { background-color: #f2f2f2; }
input[type="text"] { border: 2px solid
blue;