Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CRUD operations in Angular 7
Search
Nishu Goel
April 20, 2019
Technology
1
270
CRUD operations in Angular 7
Learn how to perform create, read, update, and delete operations in Angular 7 with Routing.
Nishu Goel
April 20, 2019
Tweet
Share
More Decks by Nishu Goel
See All by Nishu Goel
Diagnosing INP & Breaking down long tasks
nishugoel
0
400
Dear performant app,
nishugoel
0
78
The Angular Router - TrivandrumTechCon20
nishugoel
4
150
Creating Libraries in Angular
nishugoel
0
170
ngIndia - HostBinding() and HostListener()
nishugoel
0
160
Other Decks in Technology
See All in Technology
Why do you get AWS certificates
hirosys
0
120
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
8
3.9k
エンジニア候補者向け資料2024.03.28.pdf
macloud
0
2.9k
8週連続ウェビナー_イチから学ぶFivetran
cmsuzu
0
170
Terraform v1.7のTest mocking機能の紹介 / Introducing the Test mocking feature of Terraform v1.7
yayoi_dd
1
100
データ化エンジニアとしての1年を振り返る
sansantech
PRO
3
260
VSCode上からSlackにメッセージを送る拡張機能を作っている話
ebarakazuhiro
0
130
LLM + RAG を使った SORACOM Support Bot の裏側の歴史
soracom
PRO
1
650
LLMプロダクト事業の立ち上げにおける挑戦
layerx
PRO
7
1.5k
生成AIサービスPanorama AIご説明資料
sdt
0
300
現実世界の事象から学ぶSOLID原則
h0r15h0
25
11k
コードレビューを支援するAI技術の応用
akkie76
3
200
Featured
See All Featured
Making Projects Easy
brettharned
106
5.4k
Optimising Largest Contentful Paint
csswizardry
7
2.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
18
1.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
124
32k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
113
18k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
657
120k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
Ruby is Unlike a Banana
tanoku
95
10k
Making the Leap to Tech Lead
cromwellryan
123
8.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
35
2.4k
Rebuilding a faster, lazier Slack
samanthasiow
72
8.2k
Transcript
CRUD Operations in Angular 7
Nishu Goel Software Engineer, IBM | Udemy Author | Angular
Developer @DcoustaWilson Blog: https://nishugoel.wordpress.com HELLO!
GitHub Repository https://github.com/NishuGoel/CRUDwithAngular Blog post on Building a CRUD Application
with Angular https://www.c-sharpcorner.com/article/building-a-crud-application- with-angular/
CRUD?
Fake a back-end Server?
Three ways - Return data from local File - Use
local JSON file - Use Angular-in-memory-web-api
Angular in-memory-web-api
AGENDA ❑ Getting the data from the in memory data
store ❑ Reading this data ❑ Creating the data ❑ Updating the data ❑ Deleting the data
❑ Setting up the in-memory-web-api npm install angular-in-memory-web-api --save-dev ❑
Importing it in the module for the data class @NgModule({ imports: [ BrowserModule, InMemoryWebApiModule.forRoot(UserData) ] )
Using the in-memory-web-api ❑ Create the entity class export class
User { constructor ( public id = 0, public name= '', public model= 0, ) {}} createDb(){ } ❑ Providing the method to create data in the class
Data ready, Let’s perform HTTP operations! - Create Service -
Inject HttpClient service to perform the HTTP operations - Refer to the created API Perform Create, Read, Update, Delete
Create Service ng generate service <service-name> constructor(private http: HttpClient) {
} Inject http service apiurl = 'api/Users’; headers = new HttpHeaders().set('Content-Type', 'application/json').set('Accept', 'application/json'); httpOptions = { headers: this.headers }; Use the data Import required statements import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { Observable, throwError } from 'rxjs'; import { tap, catchError, map } from 'rxjs/operators'; import { User } from './User';
Read Operation Using http.get() method
getUsers(): Observable<User[]> { return this.http.get<User[]>(this.apiurl).pipe( tap(data => console.log(data)), catchError(this.handleError) );
} In the service In the Component Users: User[] = []; constructor(private dataservice: DataService) { } ngOnInit() { this.getUsers(); } getUsers() { this.dataservice.getUsers().subscribe(data => { this.Users = data; }); } }
Create Data Using http.post()
In the Service addUser (User: User): Observable<User> { return this.http.post<User>(this.apiurl,
User, this.httpOptions).pipe( tap(data => console.log(data)), catchError(this.handleError) ); } On the Component addUser() { this.dataservice.addUser(this.UserFormGroup.val ue).subscribe(data => { this.User = data; console.log(this.User); }); this.getUsers();
Update Data Using http.put()
In the Service updateUser (user: User): Observable<null | User> {
return this.http.put<User>(this.apiurl, User, this.httpOptions).pipe( tap(data => console.log(data)), catchError(this.handleError) ); } On the Component updateUser() { this.dataservice.getUser(this.idtoupdate).subscribe(data => { this.UserToUpdate = data; this.UserToUpdate.model = 'updated model'; this.dataservice.updateUser(this.UserToUpdate).subscribe(data1 => { this.getUsers(); }); });
Delete Data Using http.delete()
In the Service deleteUser (id: number): Observable<User> { const url
= `${this.apiurl}/${id}`; return this.http.delete<User>(url, this.httpOptions).pipe( catchError(this.handleError) ); } On the Component deleteUser() { this.dataservice.deleteUser(this.idtodelete).subscribe(data => { this.getUsers(); }); }
Stackblitz Demo https://stackblitz.com/github/NishuGoel/CRUDwithAngular Thank You!