This tutorial shows how to build a basic Angular CRUD application with master and detail views for listing, adding, editing and deleting records from a JSON API. The records in the example app are user records, but the same CRUD pattern and code structure could be used to manage any type of data e.g. products, services, articles etc.
The example app includes a basic home page and users section, the default page in the users section displays a list of all users and includes buttons to add, edit and delete users. The add and edit buttons navigate to a page containing a form for creating and updating user records, and the delete button executes a function within the user list component to delete a user record. The add and edit forms are both implemented with the same add/edit component which behaves differently depending on which mode it is in (“add mode” vs “edit mode”).
The example project is available on GitHub at https://github.com/cornflourblue/angular-master-details-crud-example.
Here it is in action:
(See on StackBlitz at https://stackblitz.com/edit/angular-master-details-crud-example)
npm install
or npm i
from the command line in the project root folder (where the package.json is located).npm start
from the command line in the project root folder, this will compile the Angular app and automatically launch it in the browser on the URL http://localhost:4200
.NOTE: You can also start the CRUD app with the Angular CLI command ng serve --open
. To do this first install the Angular CLI globally on your system with the command npm install -g @angular/cli
.
For more info on setting up your local Angular dev environment see Angular - Setup Development Environment.
The Angular CLI was used to generate the base project structure with the ng new <project name>
command, the CLI is also used to build and serve the application. For more info about the Angular CLI see https://angular.io/cli.
The app and code structure of the tutorial mostly follow the best practice recommendations in the official Angular Style Guide, with a few of my own tweaks here and there.
Each feature has it’s own folder (home & users), other shared/common code such as components, services, models, helpers etc are placed in folders prefixed with an underscore _
to easily differentiate them from features and group them together at the top of the folder structure.
The index.ts
files in some folders are barrel files that group the exported modules from that folder together so they can be imported using only the folder path instead of the full module path, and to enable importing multiple modules in a single import (e.g. import { UserService, AlertService } from '@app/_services'
).
The users
feature is a self contained feature module that manages its own layout, routes and components, and is hooked into the main Angular CRUD app inside the app routing module with lazy loading.
Path aliases @app
and @environments
have been configured in tsconfig.base.json that map to the /src/app
and /src/environments
directories. This allows imports to be relative to the app and environments folders by prefixing import paths with aliases instead of having to use long relative paths (e.g. import MyComponent from '../../../MyComponent'
).
Here are the main project files that contain the CRUD application logic, I left out some files that were generated by Angular CLI ng new
command that I didn’t change.
#angular #crud