AppDividend
Latest Code Tutorials

Angular 7 CRUD Example | MEAN Stack Tutorial From Scratch

433

Get real time updates directly on you device, subscribe now.

Angular 7 CRUD Example | MEAN Stack Tutorial is today’s leading topic. In this article, we will be creating a sample Angular 7 application step by step from scratch and perform CRUD operations. The backend API will be exposed using Node.js, and Express framework and MongoDB will be used for persistent storage of the data. We will be using Angular CLI 7 to generate the boilerplate project. This Angular 7 CRUD Example Tutorial is the comprehensive guide on building CRUD (Create, Read, Update, Delete) Web Application using the New Angular 7 Framework. The Angular 7 just released and it comes with a few new feature and improvements.

Angular 7 Features and Upgrades

Angular 7 got released this year in October with multiple new features such as CLI Prompts, Virtual Scroll, Drag and Drop, Angular Budgets and many more. From this release, Angular supports Node 10 and simultaneously maintains Node 8 support with TypeScript 3.1 and RxJS 6.3. A new extension from @angular/schematics called prompts has been introduced with the CLI that will now prompt developers when running common commands such as ng new my-app. Following are the list of the new Angular 7 features and upgrades.

  1. A new ng-compiler
  2. CLI prompts
  3. Angular DoBootstrap
  4. Application performance is increased
  5. ScrollingModule and DragDropModule
  6. Ivy Renderer
  7. TypeScript 3.1
  8. RxJS 6.3

Workflow of Angular 7 Tutorial

We will create two separate projects. One is for Angular, and one is for Node.js | Express | MongoDB. That means one for frontend and one for the backend. We will create a backend API, and frontend will consume that API. For this example, I am using the following tech stacks and their versions.

  1. Node v10.11.0
  2. NPM v6.4.1
  3. AngularCLI v7.0.2
  4. MongoDB shell version v3.6.3
  5. MongoDB version v3.6.3
  6. Mac OS Mojave

Angular 7 CRUD Example | MEAN Stack Tutorial

First, we will install Angular 7 using Angular CLI, and then we will continue to develop the frontend and backend.

#1: Install Angular 7 and other dependencies.

If you have an older @angular/cli version, then you can run the following command to install the latest versions.

npm uninstall -g @angular/cli
npm cache verify
npm install -g @angular/cli

If you are going through any issues, then please check out my this How To Update Angular CLI To Version 7It will help you to update your Angular CLI, and you will be able to create a brand new Angular seven project.

Okay, now if you type the following command, you can see that we have updated Angular CLI.

 

Angular 7 CRUD Example | MEAN Stack Tutorial

Now, you will be able to create a new Angular project using the following command.

ng new angular7crud
cd angular7crud

 

MEAN Stack CRUD Example

After going inside the project folder, open the project in Visual Studio Code using the following command. If you are not using it, then start using it. It is the best Editor for Javascript development.

code .

At the time of installation, we have enabled to routing for our application. It is new in Angular 7 because it will prompt us while installing angular boilerplate. You can check the file called app-routing.module.ts file inside src >> app directory.

Next, install the Bootstrap 4 CSS Framework using the following command.

npm install bootstrap --save

Now, add it inside the angular.json file.

"styles": [
   "src/styles.css",
   "./node_modules/bootstrap/dist/css/bootstrap.min.css"
 ],

So, now we can use the Bootstrap 4 classes in our project. 

Start the Angular development server using the following command.

ng serve -o

 

Related Posts
1 of 16

Angular 7 Tutorial

Project Description

We will create a project, where the user can enter their User Name, Business Name, and GST Number from the form and submit the form. If the values are incorrect, then it will validate at the frontend and form will not submit. If all the values seem perfect, then we will be able to send the form to backend API, and it will store the values inside the MongoDB database.

So now, we will create some angular components to do the job.

#2: Generate Angular Components

Type the following command to generate Angular 7 Components. We will perform create, read, update operations. So we will create three components.

ng g c gst-add --spec=false
ng g c gst-get --spec=false
ng g c gst-edit --spec=false

 

Angular CRUD Tutorial

All the three components are automatically registered inside an app.module.ts file. Now, we need to configure the routing of angular components inside an app-routing.module.ts file.

// app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { GstAddComponent } from './gst-add/gst-add.component';
import { GstEditComponent } from './gst-edit/gst-edit.component';
import { GstGetComponent } from './gst-get/gst-get.component';

const routes: Routes = [
  {
    path: 'business/create',
    component: GstAddComponent
  },
  {
    path: 'business/edit/:id',
    component: GstEditComponent
  },
  {
    path: 'business',
    component: GstGetComponent
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule { }

Now, you can see inside the app.component.html file that <router-outlet> directive is there. This directive helps us to render the different component based on the route URI.

#3: Create an Angular Navigation

Write the following code inside the app.component.html file.

<nav class="navbar navbar-expand-sm bg-light">
  <div class="container-fluid">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a routerLink="business/create" class="nav-link" routerLinkActive="active">
          Create Business
        </a>
      </li>
      <li class="nav-item">
        <a routerLink="business" class="nav-link" routerLinkActive="active">
          Business
        </a>
      </li> 
    </ul>
  </div>
</nav>

<div class="container">
  <router-outlet></router-outlet>
</div>

Save the file and go to the browser and click on two links. You can see that we can see the different components based on the navigation.

#4: Install Angular Routing Progress Indicator.

Type the following command to install the ng2-slim-loading-bar library.

npm install ng2-slim-loading-bar --save

So, if you install third-party packages right now, then it is not compatible with Angular 7. To bridge the gap between Angular 7 and third-party packages, we need to install the following library. That is it.

npm install rxjs-compat --save

Now, import the SlimLoadingBarModule inside the app.module.ts file.

// app.module.ts

import { SlimLoadingBarModule } from 'ng2-slim-loading-bar';

imports: [
    ...
    SlimLoadingBarModule
],

Next step is, include the styling that comes with the library inside src  >>  styles.css file.

@import "../node_modules/ng2-slim-loading-bar/style.css";

#5: Adding Router Events.

Angular RouterModule gives us the following event modules.

  1. NavigationStart
  2. NavigationEnd
  3. NavigationError
  4. NavigationCancel
  5. Router
  6. Event

Now, write the following code inside the  app.component.ts file.

// app.component.ts

import { Component } from '@angular/core';
import {SlimLoadingBarService} from 'ng2-slim-loading-bar';
import { NavigationCancel,
        Event,
        NavigationEnd,
        NavigationError,
        NavigationStart,
        Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular7crud';
  constructor(private _loadingBar: SlimLoadingBarService, private _router: Router) {
    this._router.events.subscribe((event: Event) => {
      this.navigationInterceptor(event);
    });
  }
  private navigationInterceptor(event: Event): void {
    if (event instanceof NavigationStart) {
      this._loadingBar.start();
    }
    if (event instanceof NavigationEnd) {
      this._loadingBar.complete();
    }
    if (event instanceof NavigationCancel) {
      this._loadingBar.stop();
    }
    if (event instanceof NavigationError) {
      this._loadingBar.stop();
    }
  }
}

What it is doing that it intercepts the routing event and add the loading bar component to every route so that we can see the routing indication every time we change the routes.

The final change to display routing indicator is that we need to add the ng2-slim-loading-bar directive inside the app.component.html file at the top of the page.

<ng2-slim-loading-bar color="blue"></ng2-slim-loading-bar>

<nav class="navbar navbar-expand-sm bg-light">
  <div class="container-fluid">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a routerLink="business/create" class="nav-link" routerLinkActive="active">
          Create Business
        </a>
      </li>
      <li class="nav-item">
        <a routerLink="business" class="nav-link" routerLinkActive="active">
          Business
        </a>
      </li> 
    </ul>
  </div>
</nav>

<div class="container">
  <router-outlet></router-outlet>
</div>

Save the file and go to the terminal to see if there any error and if not then go to the browser and change the routes, and you can see that now we can see the routing indicator.

#6: Add Bootstrap Form

Inside the gst-add.component.html file, add the following bootstrap 4 form.

<div class="card">
  <div class="card-body">
    <form>
      <div class="form-group">
        <label class="col-md-4">Person Name</label>
        <input type="text" class="form-control" />
      </div>
      <div class="form-group">
        <label class="col-md-4">Business Name </label>
        <input type="text" class="form-control" />
      </div>
      <div class="form-group">
        <label class="col-md-4">Business GST Number </label>
        <input type="text" class="form-control" />
      </div>
      <div class="form-group">
        <button type="submit" class="btn btn-primary">Add Business</button>
      </div>
    </form>
  </div>
</div>

 

Angular 7 CRUD Demo

#7: Add Angular Form Validation

We will use ReactiveFormsModule. So if you are new to Angular Form Validation, then please check out my this article Angular 7 Form Validation Example Tutorial on this blog.

Now, import the ReactiveFormsModule inside the app.module.ts file.

// app.module.ts

import { ReactiveFormsModule } from '@angular/forms';

imports: [
    ...
    ReactiveFormsModule
],

Now, we need to write the code for the app.component.ts file. Remember, this is not template-driven form. So we will change the code inside the app.component.ts file.

First, we import the FormGroup, FormBuilder, Validators modules from @angular/forms.

Also, create a constructor and instantiate the FormBuilder.

So write the following code inside the gst-add.component.ts file.

// gst-add.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup,  FormBuilder,  Validators } from '@angular/forms';

@Component({
  selector: 'app-gst-add',
  templateUrl: './gst-add.component.html',
  styleUrls: ['./gst-add.component.css']
})
export class GstAddComponent implements OnInit {

  angForm: FormGroup;
  constructor(private fb: FormBuilder) {
    this.createForm();
  }

  createForm() {
    this.angForm = this.fb.group({
      person_name: ['', Validators.required ],
      business_name: ['', Validators.required ],
      business_gst_number: ['', Validators.required ]
    });
  }

  ngOnInit() {
  }

}

We have used form builder to handle all the validation. So in that constructor, we are creating a form with the validation rules. In our example, there are three fields. If input text is empty, then it will give an error, and we need to display that error.

Now, write the following code inside the gst-add.component.html file.

<div class="card">
  <div class="card-body">
    <form [formGroup]="angForm" novalidate>
      <div class="form-group">
        <label class="col-md-4">Person Name</label>
        <input type="text" class="form-control" formControlName="person_name" #person_name />
      </div>
      <div *ngIf="angForm.controls['person_name'].invalid && (angForm.controls['person_name'].dirty || angForm.controls['person_name'].touched)" class="alert alert-danger">
        <div *ngIf="angForm.controls['person_name'].errors.required">
          Person Name is required.
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4">Business Name </label>
        <input type="text" class="form-control" formControlName="business_name" #business_name />
      </div>
      <div *ngIf="angForm.controls['business_name'].invalid && (angForm.controls['business_name'].dirty || angForm.controls['business_name'].touched)" class="alert alert-danger">
        <div *ngIf="angForm.controls['business_name'].errors.required">
          Person Business is required.
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4">Business GST Number </label>
        <input type="text" class="form-control" formControlName="business_gst_number" #business_gst_number />
      </div>
      <div *ngIf="angForm.controls['business_gst_number'].invalid && (angForm.controls['business_gst_number'].dirty || angForm.controls['business_gst_number'].touched)" class="alert alert-danger">
        <div *ngIf="angForm.controls['business_gst_number'].errors.required">
          Business GST Number is required.
        </div>
      </div>
      <div class="form-group">
        <button type="submit" 
        [disabled]="angForm.pristine || angForm.invalid" 
        class="btn btn-primary">Add Business</button>
      </div>
    </form>
  </div>
</div>

Save the file and go to the browser and you can see, if you do not put any value inside the input box, then you will see the errors.

 

Angular 7 Form Validation Example

#8: Configure the HttpClientModule

Import the HttpClientModule inside the app.module.ts file.

// app.module.ts

import { HttpClientModule } from '@angular/common/http';

imports: [
   ...
    HttpClientModule
 ],

#9: Create a model.

Inside the src >> app folder, create one file called Business.ts and add the following code.

// Business.ts

export default class Business {
  person_name: String;
  business_name: String;
  business_gst_number: Number;
}

#10: Create an Angular Service file.

Type the following command to generate the service file.

ng g service business --spec=false

So, your primary business.service.ts file looks like this.

// business.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class BusinessService {

  constructor() { }
}

Now, import the business.service.ts file into the app.module.ts file.

// app.module.ts

import { BusinessService } from './business.service';

providers: [ BusinessService ],

#11: Submit the data to the node server

Now, we need to write the code that will send the HTTP POST request with the data to the Node.js server and save the data into the MongoDB database.

Write the following code inside the business.service.ts file.

// business.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class BusinessService {

  uri = 'http://localhost:4000/business';

  constructor(private http: HttpClient) { }

  addBusiness(person_name, business_name, business_gst_number) {
    const obj = {
      person_name: person_name,
      business_name: business_name,
      business_gst_number: business_gst_number
    };
    console.log(obj);
    this.http.post(`${this.uri}/add`, obj)
        .subscribe(res => console.log('Done'));
  }
}

We have defined our backend API URL, but we have not created any backend yet, but we will do in a couple of steps.

Now, we need to add the click event to the Add Business Button. So add the following code inside the gst-add.component.html file.

<div class="form-group">
    <button (click)="addBusiness(person_name.value, business_name.value, business_gst_number.value)"
        [disabled]="angForm.pristine || angForm.invalid" 
        class="btn btn-primary">
        Add Business
     </button>
</div>

So when there are no errors, we can submit the form, and it will call the component’s addBusiness function. From there, we will call the angular service and service will send the HTTP Post request to the Node.js server.

Now, add the addBusiness function inside the gst-add.component.ts file. So write the following code inside the gst-add.component.ts file.

// gst-add.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup,  FormBuilder,  Validators } from '@angular/forms';
import { BusinessService } from '../business.service';

@Component({
  selector: 'app-gst-add',
  templateUrl: './gst-add.component.html',
  styleUrls: ['./gst-add.component.css']
})
export class GstAddComponent implements OnInit {

  angForm: FormGroup;
  constructor(private fb: FormBuilder, private bs: BusinessService) {
    this.createForm();
  }

  createForm() {
    this.angForm = this.fb.group({
      person_name: ['', Validators.required ],
      business_name: ['', Validators.required ],
      business_gst_number: ['', Validators.required ]
    });
  }

  addBusiness(person_name, busines_name, business_gst_number) {
    this.bs.addBusiness(person_name, busines_name, business_gst_number);
  }

  ngOnInit() {
  }

}

Here, we have defined the function and also imported the business.service.ts file. Instantiate the object inside the constructor and use that to call the function of the businsess.service.ts file.

We have already coded the addBusiness function inside the business.service.ts file. Now, we need to configure the backend API.

#12: Create a Node.js backend API

Inside the angular root folder, create one folder called api and go inside that folder. Remember, it will be a completely separate project from Angular. So its node_modules are different from an Angular.

Open the terminal inside the api folder and type the following command.

npm init -y

Install the following node specific modules.

npm install --save express body-parser cors mongoose

I do not restart node server each time; I change the file. So I am installing the nodemon server. What it does is that, when I modify the server.js file, it restarts the node.js server automatically.

npm install nodemon --save-dev

Now, inside the api folder, create one file called the server.js file.

// server.js

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    mongoose = require('mongoose');

    const app = express();
    let port = process.env.PORT || 4000;

    const server = app.listen(function(){
        console.log('Listening on port ' + port);
    });

The next thing is to connect MongoDB database with our node.js application.

If you have not installed the MongoDB database then install it and then start the mongodb server.

Type the following command to start the MongoDB server.

mongod

So, Now, I have connected to the database.

Create one file called DB.js inside api root project folder. Write the following code inside the DB.js file.

// DB.js

module.exports = {
    DB: 'mongodb://localhost:27017/ng7crud'
 };

Import this DB.js file inside our server.js file and use mongoose library to set up the database connection with MongoDB. We can also use Mongoose to save the data in the database using Mongoose ORM.

Write the following code inside the server.js file to connect our MongoDB application to the Node.js server.

// server.js

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    mongoose = require('mongoose'),
    config = require('./DB');

    mongoose.Promise = global.Promise;
    mongoose.connect(config.DB, { useNewUrlParser: true }).then(
      () => {console.log('Database is connected') },
      err => { console.log('Can not connect to the database'+ err)}
    );

    const app = express();
    app.use(bodyParser.json());
    app.use(cors());
    const port = process.env.PORT || 4000;

    const server = app.listen(port, function(){
     console.log('Listening on port ' + port);
    });

Save the file and go to the terminal and start the node server.

nodemon server

So, right now, you have three servers are running.

  1. Angular Development Server
  2. Nodemon server
  3. MongoDB server

Remember all the three servers are running fine without any error otherwise, our application will not work.

#13: Create model and routes for our application.

Now, we need to create two folders inside the root folder called routes and models.

In models folder, create one model called Business.js.

// Business.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Define collection and schema for Business
let Business = new Schema({
  person_name: {
    type: String
  },
  business_name: {
    type: String
  },
  business_gst_number: {
    type: Number
  }
},{
    collection: 'business'
});

module.exports = mongoose.model('Business', Business);

So, we have defined our schema for the business collection. We have three fields called person_name, business_name, business_gst_number.

In the routes folder, create one file called the business.route.js.

Write the CRUD code inside the business.route.js file.

// business.route.js

const express = require('express');
const app = express();
const businessRoutes = express.Router();

// Require Business model in our routes module
let Business = require('../models/Business');

// Defined store route
businessRoutes.route('/add').post(function (req, res) {
  let business = new Business(req.body);
  business.save()
    .then(business => {
      res.status(200).json({'business': 'business in added successfully'});
    })
    .catch(err => {
    res.status(400).send("unable to save to database");
    });
});

// Defined get data(index or listing) route
businessRoutes.route('/').get(function (req, res) {
    Business.find(function (err, businesses){
    if(err){
      console.log(err);
    }
    else {
      res.json(businesses);
    }
  });
});

// Defined edit route
businessRoutes.route('/edit/:id').get(function (req, res) {
  let id = req.params.id;
  Business.findById(id, function (err, business){
      res.json(business);
  });
});

//  Defined update route
businessRoutes.route('/update/:id').post(function (req, res) {
    Business.findById(req.params.id, function(err, next, business) {
    if (!business)
      return next(new Error('Could not load Document'));
    else {
        business.person_name = req.body.person_name;
        business.business_name = req.body.business_name;
        business.business_gst_number = req.body.business_gst_number;

        business.save().then(business => {
          res.json('Update complete');
      })
      .catch(err => {
            res.status(400).send("unable to update the database");
      });
    }
  });
});

// Defined delete | remove | destroy route
businessRoutes.route('/delete/:id').get(function (req, res) {
    Business.findByIdAndRemove({_id: req.params.id}, function(err, business){
        if(err) res.json(err);
        else res.json('Successfully removed');
    });
});

module.exports = businessRoutes;

Here, we have used the mongoose model to save, update, delete the data from the database. Mongoose is an ORM used in MongoDB database. Now, we have all the CRUD operations set up on the route file; we need to import inside the server.js file.

So, our final server.js file looks like this.

// server.js

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    mongoose = require('mongoose'),
    config = require('./DB');

const businessRoute = require('./routes/business.route');
mongoose.Promise = global.Promise;
mongoose.connect(config.DB, { useNewUrlParser: true }).then(
  () => {console.log('Database is connected') },
  err => { console.log('Can not connect to the database'+ err)}
);

const app = express();
app.use(bodyParser.json());
app.use(cors());
app.use('/business', businessRoute);
const port = process.env.PORT || 4000;

const server = app.listen(port, function(){
  console.log('Listening on port ' + port);
});

#14: Test the store data functionality

If your all the servers are up and running then you can go to the browser and fill the form data and add the business. You can see something like this on your screen if you are successful.

 

MEAN Stack Tutorial

Now, we can check on the database using the following commands.

First, open the mongo shell on the 4th tab because all the other three tabs are occupied at the moment.

mongo

 

MongoDB CRUD

Here, we can see that the values are storing in the MongoDB database. Yess!! We have succeeded.

Now, remaining operations are Read, Update, and Delete.

#15: Display the data on frontend

In the gst-get.component.html file, write the following code.

<table class="table table-hover">
  <thead>
  <tr>
      <td>Person Name</td>
      <td>Business Name</td>
      <td>GST Number</td>
      <td colspan="2">Actions</td>
  </tr>
  </thead>

  <tbody>
      <tr *ngFor="let business of businesses">
          <td>{{ business.person_name }}</td>
          <td>{{ business.business_name }}</td>
          <td>{{ business.business_gst_number }}</td>
          <td><a [routerLink]="['/edit', business._id]" class="btn btn-primary">Edit</a></td>
          <td><a [routerLink]="" class="btn btn-danger">Delete</a></td>
      </tr>
  </tbody>
</table>

Now, inside the business.service.ts file, we need to write the function that fetches the businesses data from the MongoDB database and display at the Angular application.

// business.service.ts

getBusinesses() {
    return this
           .http
           .get(`${this.uri}`);
  }

Now, we need to include this business.service.ts file and Business.ts file inside the gst-get.component.ts file.

Write the following code inside the gst-get.component.ts file.

// gst-get.component.ts

import { Component, OnInit } from '@angular/core';
import Business from '../Business';
import { BusinessService } from '../business.service';

@Component({
  selector: 'app-gst-get',
  templateUrl: './gst-get.component.html',
  styleUrls: ['./gst-get.component.css']
})
export class GstGetComponent implements OnInit {

  businesses: Business[];

  constructor(private bs: BusinessService) { }

  ngOnInit() {
    this.bs
      .getBusinesses()
      .subscribe((data: Business[]) => {
        this.businesses = data;
    });
  }
}

Save the file and go to the browser and switch to this URL: http://localhost:4200/business. You can see the listing of the businesses.

#16: Edit and Update Data

Okay, first, we need to fetch the data from the MongoDB database using _id wise and display that data in the gst-edit.component.html file.

So first, write the following code inside the gst-edit.component.ts file.

// gst-edit.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormGroup,  FormBuilder,  Validators } from '@angular/forms';
import { BusinessService } from '../business.service';

@Component({
  selector: 'app-gst-edit',
  templateUrl: './gst-edit.component.html',
  styleUrls: ['./gst-edit.component.css']
})
export class GstEditComponent implements OnInit {

  business: any = {};
  angForm: FormGroup;

  constructor(private route: ActivatedRoute,
    private router: Router,
    private bs: BusinessService,
    private fb: FormBuilder) {
      this.createForm();
 }

  createForm() {
    this.angForm = this.fb.group({
        person_name: ['', Validators.required ],
        business_name: ['', Validators.required ],
        business_gst_number: ['', Validators.required ]
      });
    }


  ngOnInit() {
    this.route.params.subscribe(params => {
        this.bs.editBusiness(params['id']).subscribe(res => {
          this.business = res;
      });
    });
  }
}

Here, when the gst-edit component.ts render, it will call the ngOnInit method and send an HTTP request to the node server and fetch the data from a _id to display inside the gst-edit.component.html file.

Now, inside the business.service.ts file, we need to code the editBusiness function to send an HTTP request.

// business.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class BusinessService {

  uri = 'http://localhost:4000/business';

  constructor(private http: HttpClient) { }

  addBusiness(person_name, business_name, business_gst_number) {
    const obj = {
      person_name: person_name,
      business_name: business_name,
      business_gst_number: business_gst_number
    };
    this.http.post(`${this.uri}/add`, obj)
        .subscribe(res => console.log('Done'));
  }

  getBusinesses() {
    return this
           .http
           .get(`${this.uri}`);
  }

  editBusiness(id) {
    return this
            .http
            .get(`${this.uri}/edit/${id}`);
    }
}

Now, finally, we need to write the form inside the gst-edit.component.html file.

<div class="card">
  <div class="card-body">
    <form [formGroup]="angForm" novalidate>
      <div class="form-group">
        <label class="col-md-4">Person Name</label>
        <input type="text" class="form-control" formControlName="person_name" #person_name [(ngModel)] = "business.person_name" />
      </div>
      <div *ngIf="angForm.controls['person_name'].invalid && (angForm.controls['person_name'].dirty || angForm.controls['person_name'].touched)" class="alert alert-danger">
        <div *ngIf="angForm.controls['person_name'].errors.required">
          Person Name is required.
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4">Business Name </label>
        <input type="text" class="form-control" formControlName="business_name" #business_name [(ngModel)] = "business.business_name" />
      </div>
      <div *ngIf="angForm.controls['business_name'].invalid && (angForm.controls['business_name'].dirty || angForm.controls['business_name'].touched)" class="alert alert-danger">
        <div *ngIf="angForm.controls['business_name'].errors.required">
          Person Business is required.
        </div>
      </div>
      <div class="form-group">
        <label class="col-md-4">Business GST Number </label>
        <input type="text" class="form-control" formControlName="business_gst_number" #business_gst_number [(ngModel)] = "business.business_gst_number" />
      </div>
      <div *ngIf="angForm.controls['business_gst_number'].invalid && (angForm.controls['business_gst_number'].dirty || angForm.controls['business_gst_number'].touched)" class="alert alert-danger">
        <div *ngIf="angForm.controls['business_gst_number'].errors.required">
          Business GST Number is required.
        </div>
      </div>
      <div class="form-group">
        <button (click)="updateBusiness(person_name.value, business_name.value, business_gst_number.value)"
        [disabled]="angForm.invalid" 
        class="btn btn-primary">Update Business</button>
      </div>
    </form>
  </div>
</div>

Save the file and go to the listing page and click on the edit button and you will see the populated form from the database.

You can also see the warning like the following. Ignore for this demo tutorial.

forms.js:1193
It looks like you’re using ngModel on the same form field as formControlName.
Support for using the ngModel input property and ngModelChange event with
reactive form directives have been deprecated in Angular v6 and will be removed
in Angular v7.
Now, update the data. Inside the business.service.ts file, we need to write the function that updates the data.
// business.service.ts

updateBusiness(person_name, business_name, business_gst_number, id) {

    const obj = {
        person_name: person_name,
        business_name: business_name,
        business_gst_number: business_gst_number
      };
    this
      .http
      .post(`${this.uri}/update/${id}`, obj)
      .subscribe(res => console.log('Done'));
  }

Okay, now write the updateBusiness() function inside gst-edit.component.ts file.

// gst-edit.component.ts

updateBusiness(person_name, business_name, business_gst_number) {
   this.route.params.subscribe(params => {
      this.bs.updateBusiness(person_name, business_name, business_gst_number, params['id']);
      this.router.navigate(['business']);
});

Save the file, and you will be able to update the data.

#17: Delete the data.

So, if you find no error on the console, then you can successfully update the data.

 I have already written edit and update service to make an API calls. So till now, Create, Read, Update is complete of this Angular 7 CRUD Example Tutorial. Now, take a look at Delete.

Now, we need to define click event on the delete button inside the gst-get.component.html file.

<tr *ngFor="let business of businesses">
          <td>{{ business.person_name }}</td>
          <td>{{ business.business_name }}</td>
          <td>{{ business.business_gst_number }}</td>
          <td><a [routerLink]="['edit', business._id]" class="btn btn-primary">Edit</a></td>
          <td><a (click) = "deleteBusiness(business._id)" class="btn btn-danger">Delete</a></td>
</tr>

Now, write the deleteBusiness function inside the gst-get.component.ts file.

// gst-get.component.ts

deleteBusiness(id) {
    this.bs.deleteBusiness(id).subscribe(res => {
      console.log('Deleted');
    });
  }

Finally, create deleteBusiness() function inside the business.service.ts file.

// business.service.ts

deleteBusiness(id) {
    return this
              .http
              .get(`${this.uri}/delete/${id}`);
  }

Finally, Completed the delete functionality.

So, in this tutorial, we have complete the CRUD Functionality in Angular 7.

If you have any doubt in this Angular 7 CRUD Example, then ask in a comment below.

Github Code

8 Comments
  1. Dobby says

    Why didn’t you use angular material instead of bootstrap? It would be very nice article for me. Anyway, thanks for this article tho.

  2. Poly says

    Why didn’t you use Angular Material instead of bootstrap? it would be very nice. Thanks anyway.

  3. Doly says

    Why didn’t you use Angular Material instead of bootstrap? it would be very nice. Thank you!

    1. Krunal says

      Will use for future projects.

  4. lamasbr says

    Firstly, I would like to thank you for this article. I am learning Angular for the first time now and your article was very helpful.

    I had a problem with the edit form. When I click on Edit button, change any field and submit, the following error was displayed in the console: “Form submission canceled because the form is not connected”. To workaround this, was necessary add type=”button” on the submit button.

    Now I have another issue, when I submit the edit form with new values, I’m redirected to the initial page but to see the new values, I need to refresh manually. How can I fix it?

    Thank you,
    Cheers from Brazil.

  5. þorN says

    Thanks a lot, it works for me. I had to change the link in the list to the edit component from /edit to /business/edit

  6. Aravind says

    I’m getting this error ,
    Can’t able to find where i went wrong
    `this.bs.addBusiness is not a function
    at GstAddComponent.push../src/app/gst-add/gst-add.component.ts.GstAddComponent.addBusiness (gst-add.component.ts:30)`

    1. Krunal says

      You need to add the addBusiness function. You can refer my GitHub code.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.