Step by Step Guide to Creating an Angular 15 CRUD App with Material UI and JSON-Server

Posted by


In this tutorial, we will walk through the process of creating a CRUD (Create, Read, Update, Delete) application using Angular 15, Material UI, and JSON-server. This tutorial will guide you step by step on how to set up the project, create the necessary components, and implement CRUD operations.

Prerequisites:

  1. Basic knowledge of Angular, HTML, CSS, and TypeScript.
  2. Node.js and npm installed on your machine.
  3. Angular CLI installed globally.

Step 1: Set up the project

  1. Create a new Angular project by running the following command in your terminal:

    ng new angular-crud-material-ui
  2. Change directory to the newly created project:

    cd angular-crud-material-ui
  3. Install Material UI and Angular Material dependencies:

    ng add @angular/material
  4. Install JSON-server to create a mock backend for our application:
    npm install -g json-server

Step 2: Create JSON-server mock data

  1. Create a new file named db.json in the root of your project.
  2. Add mock data to the db.json file to simulate a RESTful API. For example:

    {
    "posts": [
    { "id": 1, "title": "Post 1", "content": "Lorem ipsum dolor sit amet" },
    { "id": 2, "title": "Post 2", "content": "Consectetur adipiscing elit" }
    ]
    }
  3. Start the JSON-server by running the following command:
    json-server --watch db.json

Step 3: Create Angular components

  1. Generate four new components for the CRUD operations:

    ng generate component post-list
    ng generate component post-add
    ng generate component post-edit
    ng generate component post-detail
  2. Create a service to interact with the JSON-server:
    ng generate service post

Step 4: Implement CRUD operations

  1. Update the post service to make HTTP requests to the JSON-server. Use the Angular HttpClient module to send GET, POST, PUT, and DELETE requests.

  2. Implement the CRUD operations in the post-list, post-add, post-edit, and post-detail components.

  3. Display the posts in the post-list component using Material UI components like tables or cards.

  4. Create forms in the post-add and post-edit components to add new posts and edit existing posts.

  5. Implement navigation between components using Angular Router.

Step 5: Testing the application

  1. Start the Angular development server by running the following command:

    ng serve
  2. Open your browser and navigate to http://localhost:4200 to see the application running.

  3. Test the CRUD operations by adding, editing, and deleting posts.

Congratulations! You have successfully created a CRUD application using Angular 15, Material UI, and JSON-server. You can further customize the application by adding features like authentication, pagination, or search functionality. Thank you for following this tutorial, and happy coding!

0 0 votes
Article Rating
46 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@nxgplay6616
3 months ago

i am not able to use color property in mat-toolbar can anyone suggest ? , i have to use class for this

@peenoiserocks3489
3 months ago

what could be wrong in my code. the primary color is not changing?

@shibamjha-0279
3 months ago

i am facing cors error while fetching request from localhost json server, please help me to solve

@saptanilchowdhury2600
3 months ago

Hi I am getting HttpResponse error kindly look into the error so that I can fix my error :- emp-add-edit.component.ts:48 HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: 'Unknown Error', url: 'http://localhost:3000/employees', ok: false, …}error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: 'error', …}headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}message: "Http failure response for http://localhost:3000/employees: 0 Unknown Error"name: "HttpErrorResponse"ok: falsestatus: 0statusText: "Unknown Error"url: "http://localhost:3000/employees"%5B%5BPrototype%5D%5D: HttpResponseBase and fix it for me

@pankajmishra8748
3 months ago

Sir is it normal to increase in heart rate up to 115-120 after eating also why heart beat increases when going in sunlight,is this normal,Thanks in advance

@Nivedhitha_JK8
3 months ago

Really helpful, and i got stuck my id is not in order like 1st id it's 4557 and 2nd is e616. can anyone share wat do to about this?

@surisurendrababu
3 months ago

color:"primary" is not working in angular 18

@YoursAnil9333
3 months ago

very good explanation bro!…

@jonibektoirov9138
3 months ago

Brat gap yo rahmat at dushi

@schelldaniel
3 months ago

Thank you very much for this awesome and easy-to-go tutorial.
Is it possible to getting it working github-pages or any other free hosting?
How the json server will run on any hosting?

@DanielGustavoVespo
3 months ago

great lesson, but not include validators in the field text. If the user wish include numbers in place, her can do. But it is not correct action.

@b_340anuragingole3
3 months ago

nice work but after exporting source code save button is not working help me regarding this

@jakovtrbara9509
3 months ago

Thank you very much Sir. This was the best tutorial I've ever seen on CRUD. I am a beginner so this was really helpful.

@sivas3656
3 months ago

Really useful video❤, can you do that using backend api from using visual studio also

@saidurgaprasade1932
3 months ago

db.josn file is not showing in angular 16 how to fix this

@utkarshgautam1940
3 months ago

i totally loved it,everything u taugh is working fine.the way u explain everything is very nice,thnaks you

@tejaspatil7350
3 months ago

35:33 Json Server Installation

@harshitnigam18
3 months ago

43:33

@bolivar.13
3 months ago

incredible work , thank you

@aadityaagrawal11
3 months ago

Great work!!