Introduction
In this article, we will try to implement the basic operations ( Create, Read, Update, Delete ) using Angular and WebApi.
We will be using the complex object on which all the operations are going to be performed.
Pre-requisites
Before you go ahead you must know some basics of Angular and webapi. Below are some links that are helpful before you start with the article and application implementation.
- Getting Started With Angular CLI
- Overview Of Components In Angular
- HTTP And Observables In Angular
- Overview Of Route Parameters In Angular
Implementation
Open Visual Studio Code and create the application TestApp.
Note. We have used angular CLI commands for the generation of Applications, Components, and services. Also, this is a time-consuming and long article, go through this only if you have enough time.
Eg. for a component generation we have used “ng g c addStudent”
So you have to keep in mind to generate the components/services/applications properly. Or go to my previous angular articles to see the use of the angular CLI command.
Now create a new application “ng new TestApp”.
Modal creation
Create addressClass.ts
export class AddressClass {
constructor(
public street: string,
public city: string,
public state: string
){}
}
import { AddressClass } from "./addressClass";
Create studentClass.ts
import { AddressClass } from "./addressClass";
export class StudentClass {
constructor(
public id: number,
public name: string,
public email: string,
public phone: number,
public gender: string,
public course: string,
public sendUpdates: boolean,
public bloodGroup: string,
public address: AddressClass
){}
}
Components creation
Let us create some components.
Generate component home ( “ng g c home”)
Open home.component.html and edit with the below contents.
<h1>Home page!</h1>
<p>
Students Records Management.
</p>
Generate component not found. ( “ng g c notFound”)
Open not-found.component.html and edit with the below contents.
<h2>Not found!</h2>
<p>The page you are looking for is not available.</p>
Generate component addStudent. ( “ng g c addStudent”)
Open add-student.component.html and edit with the below contents.
<div class="backgroundColor container-fluid" >
<h3>
Student Registration!
</h3>
<p>Enter the below fields for student details.</p>
<form #studentForm="ngForm" (ngSubmit)="SubmitDetails(studentForm.value)">
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" #controlName='ngModel' required [class.is-invalid]="controlName.invalid && controlName.touched" class="form-control" [(ngModel)]="studentModel.name" name="name">
<small [class.d-none]="controlName.valid || controlName.untouched" style="color:red">*Field is required</small>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="email">Email address:</label>
<input type="text" class="form-control" [(ngModel)]="studentModel.email" name="email">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="phone">Phone:</label>
<input type="tel" #controlPhone="ngModel" required pattern="^\d{10}$" [class.is-invalid]="controlPhone.invalid && controlPhone.touched" class="form-control" [(ngModel)]="studentModel.phone" name="phone" />
<div *ngIf="controlPhone.errors && (controlPhone.invalid && controlPhone.touched)">
<small *ngIf="controlPhone.errors.required" class="text-danger">*This field is required</small>
<small *ngIf="controlPhone.errors.pattern" class="text-danger">*Must be 10 digit</small>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="gender">gender:</label>
<input type="text" class="form-control" [(ngModel)]="studentModel.gender" name="gender">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="bloodGroup">Blood Group:</label>
<select required #bloodGroup="ngModel" value="Select" class="form-control" [class.is-invalid]="bloodGroup.invalid && bloodGroup.touched" [(ngModel)]="studentModel.bloodGroup" name="bloodGroup">
<option value="">Select Blood Group</option>
<option *ngFor="let group of groups" >{{group}}</option>
</select>
<small class="text-danger" *ngIf="bloodGroup.invalid && bloodGroup.touched">*Please select Blood group</small>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Course </label><br>
<input type="radio" [(ngModel)]="studentModel.course" name="course" value="BCA">BCA
<input type="radio" [(ngModel)]="studentModel.course" name="course" value="B.Tech">B.Tech
<input type="radio" [(ngModel)]="studentModel.course" name="course" value="BE">BE
<input type="radio" [(ngModel)]="studentModel.course" name="course" value="B.Sc">B.Sc
</div>
</div>
</div>
<div class="row" ngModelGroup="address">
<div class="col-sm-4">
<div class="form-group">
<label>Street</label>
<input type="text" class="form-control" name="street" [(ngModel)]="studentModel.address.street">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>City</label>
<input type="text" class="form-control" name="city" [(ngModel)]="studentModel.address.city">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>State</label>
<input type="text" class="form-control" name="state" [(ngModel)]="studentModel.address.state">
</div>
</div>
</div>
<div class="checkbox">
<label><input type="checkbox" [(ngModel)]="studentModel.sendUpdates" name="sendUpdates"> send me updates</label>
</div>
<input type="hidden" [(ngModel)]="studentModel.id" name="id">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
Open add-student.component.css and edit with the below contents.
.backgroundColor {
background-color: #d5f5f3 !important;
}
Open add-student.component.ts and edit with the below contents.
import { Component, OnInit } from '@angular/core';
import { AddressClass } from '../addressClass';
import { EnrollmentService } from '../enrollment-service.service';
import { StudentClass } from '../studentClass';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-add-student',
templateUrl: './add-student.component.html',
styleUrls: ['./add-student.component.css']
})
export class AddStudentComponent implements OnInit {
groups=['A+', 'A-', 'B+', 'B-', 'O+', 'O-'];
addressModel = new AddressClass("", "", "");
studentModel = new StudentClass(0,"", "", null, "", "", true, "", this.addressModel)
constructor(private _enrollmentService: EnrollmentService, private route: ActivatedRoute, private router : Router) {
console.log("constructor calling..");
}
SubmitDetails(studentData){
console.log(studentData);
if(studentData.id == 0){
this._enrollmentService.addStudent(studentData)
.subscribe(
(data) => {
console.log('success', data);
this.router.navigate(['/studentList']);
},
error => console.log('Error', error)
);
}else
{
this._enrollmentService.editStudent(studentData)
.subscribe(
(data) => {
console.log('success', data);
this.router.navigate(['/studentList']);
},
error => console.log('Error', error)
);
}
}
ngOnInit() {
console.log("ngOnInit method calling..");
var id = parseInt(this.route.snapshot.paramMap.get('id'));
this._enrollmentService.getStudent(id).subscribe(data => this.studentModel = data);
}
}
Generate component studentDetails
Open student-details.component.html and edit with the below contents.
<h2>Selected Student Details</h2>
<h5>This is the details for <b>{{studentModel.name}}</b></h5>
<div><b>Id: </b>{{studentModel.id}}</div>
<div><b>Email Address: </b>{{studentModel.email}}</div>
<div><b>Phone: </b>{{studentModel.phone}}</div>
<div><b>Gender: </b>{{studentModel.gender}}</div>
<div><b>Blood group: </b>{{studentModel.bloodGroup}}</div>
<div><b>Course: </b>{{studentModel.course}}</div>
<div><b>Street: </b>{{studentModel.address.street}}</div>
<div><b>City: </b>{{studentModel.address.city}}</div>
<div><b>State: </b>{{studentModel.address.state}}</div>
<div><a (click)="gotoStudentList()">Back</a></div>
Open student-details.component.ts and edit with the below contents.
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { StudentClass } from '../studentClass';
import { EnrollmentService } from '../enrollment-service.service';
@Component({
selector: 'app-student-details',
templateUrl: './student-details.component.html',
styleUrls: ['./student-details.component.css']
})
export class StudentDetailsComponent implements OnInit {
studentModel: StudentClass;
constructor(private _enrollmentService: EnrollmentService, private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
var id = parseInt(this.route.snapshot.paramMap.get('id'));
this._enrollmentService.getStudent(id).subscribe(data => this.studentModel = data);
}
gotoStudentList() {
this.router.navigate(['/studentList']);
}
showFees() {
this.router.navigate(['fees'], { relativeTo: this.route });
}
showAddress() {
this.router.navigate(['address'], { relativeTo: this.route });
}
}
Generate studentList component
Open student-list.component.html and edit with the below contents.





Sani YadavPosted Apr 17, 2020, 12:17 PM
Very good article
Sani YadavPosted Apr 17, 2020, 12:14 PM
Very good article
Mahesh AllePosted Dec 3, 2019, 6:48 AM
Good Article. It was clear explained with proper front end and back-end code. Thank you for posting this article. Keep it up.....
Vidyadharran GPosted Nov 27, 2019, 12:00 AM
Neatly explained ....thanks bro
Sudhakar MandalPosted Nov 24, 2019, 6:27 AM
Interesting article. Good old fashion Ado.net is used, when most them use entity framework these days. Kudos!
Vijayakumar ManiPosted Nov 23, 2019, 7:41 AM
Very useful one.. Thanks Mohammad
Sourav Kumar DasPosted Nov 23, 2019, 12:45 AM
Nice useful article Sir.
Chittaranjan SwainPosted Nov 22, 2019, 9:26 AM
Nice article.