So far, we have covered Web API, Angular Routing, and Services. Now let’s implement CRUD operations in Angular components.
First install the given packages, because I am going to use these in my sample application.
Bootstrap
npm install bootstrap –-save
JQuery
npm install jquery --save
Material JS
npm install --save @angular/material
Font Awesome
npm install font-awesome –save
Some important angular cli commands,
To create new class
ng generate class Employee
To create new service
ng generate service employee
To create new component
ng generate component header
To run the application
ng serve
Once you've installed the above-given packages, you need to add q reference of Bootstrap in Angular.json like this:
- "styles": [
- "src/styles.scss",
- "node_modules/bootstrap/dist/css/bootstrap.min.css"
- ],
- "scripts": [
- "node_modules/jquery/dist/jquery.min.js",
- "node_modules/bootstrap/dist/js/bootstrap.min.js"
- ]
Add stylesheets refrence in styles.scss file.
-
- @import "~bootstrap/dist/css/bootstrap.css";
-
- @import '@angular/material/prebuilt-themes/deeppurple-amber.css';
Go to terminal and create a new class:
- export class Employee {
- EmployeeID: number;
- LastName: string;
- FirstName: string;
- Title: string;
- TitleOfCourtesy: string;
- BirthDate: Date;
- HireDate: Date;
- Address: string;
- City: string;
- Region: string;
- PostalCode: string;
- Country: string;
- HomePhone: string;
- Extension: string;
- Photo: string;
- Notes: string;
- }
Add a new component
Add the following code in component and add the service and class refrence and CRUD functions in component.
- import { Component, OnInit } from '@angular/core';
- import { FormBuilder, Validators } from '@angular/forms';
- import { Observable } from 'rxjs';
- import { EmployeeService } from '../employee.service';
- import { Employee } from '../employee';
-
- @Component({
- selector: 'app-employee',
- templateUrl: './employee.component.html',
- styleUrls: ['./employee.component.scss']
- })
- export class EmployeeComponent implements OnInit {
- dataSaved = false;
- employeeForm: any;
- allEmployees: Observable<Employee[]>;
- employeeIdUpdate = null;
- massage = null;
-
- constructor(private formbulider: FormBuilder,
- private employeeService: EmployeeService) { }
-
- ngOnInit() {
- this.employeeForm = this.formbulider.group({
- LastName: ['', [Validators.required]],
- FirstName: ['', [Validators.required]],
- Title: ['', ''],
- TitleOfCourtesy: ['', ''],
- BirthDate: ['', [Validators.required]],
- HireDate: ['', ''],
- Address: ['', [Validators.required]],
- City: ['', [Validators.required]],
- Region: ['', [Validators.required]],
- PostalCode: ['', [Validators.required]],
- Country: ['', [Validators.required]],
- HomePhone: ['', [Validators.required]],
- Notes: ['', [Validators.required]],
- });
- this.loadEmployees();
- }
- loadEmployees() {
- this.allEmployees = this.employeeService.getEmployees();
- }
- onFormSubmit() {
- this.dataSaved = false;
- const employee = this.employeeForm.value;
- this.CreateEmployee(employee);
- this.employeeForm.reset();
- }
- loadEmployee(employeeId: string) {
- this.employeeService.getEmployee(employeeId).subscribe(employee => {
- this.massage = null;
- this.dataSaved = false;
- this.employeeIdUpdate = employee.EmployeeID;
- this.employeeForm.controls['LastName'].setValue(employee.LastName);
- this.employeeForm.controls['FirstName'].setValue(employee.FirstName);
- this.employeeForm.controls['Title'].setValue(employee.Title);
- this.employeeForm.controls['TitleOfCourtesy'].setValue(employee.TitleOfCourtesy);
- this.employeeForm.controls['BirthDate'].setValue(employee.BirthDate);
- this.employeeForm.controls['HireDate'].setValue(employee.HireDate);
- this.employeeForm.controls['Address'].setValue(employee.Address);
- this.employeeForm.controls['City'].setValue(employee.City);
- this.employeeForm.controls['Region'].setValue(employee.Region);
- this.employeeForm.controls['PostalCode'].setValue(employee.PostalCode);
- this.employeeForm.controls['Country'].setValue(employee.Country);
- this.employeeForm.controls['HomePhone'].setValue(employee.HomePhone);
- this.employeeForm.controls['Notes'].setValue(employee.Notes);
- });
-
- }
- CreateEmployee(employee: Employee) {
- if (this.employeeIdUpdate == null) {
- this.employeeService.createEmployee(employee).subscribe(
- () => {
- this.dataSaved = true;
- this.massage = 'Record saved Successfully';
- this.loadEmployees();
- this.employeeIdUpdate = null;
- this.employeeForm.reset();
- }
- );
- } else {
- employee.EmployeeID = this.employeeIdUpdate;
- this.employeeService.updateEmployee(employee).subscribe(() => {
- this.dataSaved = true;
- this.massage = 'Record Updated Successfully';
- this.loadEmployees();
- this.employeeIdUpdate = null;
- this.employeeForm.reset();
- });
- }
- }
-
- deleteEmployee(employeeId: string) {
- if (confirm("Are you sure you want to delete this ?")) {
- this.employeeService.deleteEmployee(employeeId).subscribe(() => {
- this.dataSaved = true;
- this.massage = 'Record Deleted Succefully';
- this.loadEmployees();
- this.employeeIdUpdate = null;
- this.employeeForm.reset();
- });
- }
- }
- resetForm() {
- this.employeeForm.reset();
- this.massage = null;
- this.dataSaved = false;
- }
- }
Add the following html in employee.compnent.html:
Add the component and class and other references in app.module.ts file.
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
- import { HttpClientModule, HttpClient } from '@angular/common/http';
- import { FormsModule, ReactiveFormsModule } from '@angular/forms';
- import {
- MatButtonModule, MatMenuModule, MatDatepickerModule, MatNativeDateModule , MatIconModule, MatCardModule, MatSidenavModule, MatFormFieldModule,
- MatInputModule, MatTooltipModule, MatToolbarModule
- } from '@angular/material';
- import { MatRadioModule } from '@angular/material/radio';
- import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
-
- import { AppRoutingModule } from './app-routing.module';
- import { AppComponent } from './app.component';
- import { NavbarComponent } from './navbar/navbar.component';
- import { EmployeeComponent } from './employee/employee.component';
- import { AboutComponent } from './about/about.component';
- import { FooterComponent } from './footer/footer.component';
- import { HeaderComponent } from './header/header.component';
- import { EmployeeService } from './employee.service';
-
- @NgModule({
- declarations: [
- AppComponent,
- NavbarComponent,
- EmployeeComponent,
- AboutComponent,
- FooterComponent,
- HeaderComponent
- ],
- imports: [
- BrowserModule,
- AppRoutingModule,
- FontAwesomeModule,
- HttpClientModule,
- FormsModule,
- ReactiveFormsModule,
- HttpClientModule,
- BrowserAnimationsModule,
- MatButtonModule,
- MatMenuModule,
- MatDatepickerModule,
- MatNativeDateModule,
- MatIconModule,
- MatRadioModule,
- MatCardModule,
- MatSidenavModule,
- MatFormFieldModule,
- MatInputModule,
- MatTooltipModule,
- MatToolbarModule,
- ],
- providers: [EmployeeService, HttpClientModule, MatDatepickerModule],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Run the application.
Open http://localhost:4200/ in browser.
On the page, you have a list of employees and options to create a new employee and edit an existing employee and delete an employee. As you can see, we have used Bootstrap and material.js for layout styling. For example, when you click on a last name then then you will see tooltip and validation color.
Click on the little calendar icon to see the calendar.
Conclusion
In this article, we have learned the CRUD operations using Angular with Web API and Entity Framework with SQL Server data. I have attached sample application source code and Web API code separately.