$30 off During Our Annual Pro Sale. View details »

CRUD operations in Angular 7

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

More Decks by Nishu Goel

Other Decks in Technology

Transcript

  1. CRUD Operations in Angular 7

  2. Nishu Goel Software Engineer, IBM | Udemy Author | Angular

    Developer @DcoustaWilson Blog: https://nishugoel.wordpress.com HELLO!
  3. 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/
  4. CRUD?

  5. Fake a back-end Server?

  6. Three ways - Return data from local File - Use

    local JSON file - Use Angular-in-memory-web-api
  7. Angular in-memory-web-api

  8. AGENDA ❑ Getting the data from the in memory data

    store ❑ Reading this data ❑ Creating the data ❑ Updating the data ❑ Deleting the data
  9. ❑ 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) ] )
  10. 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
  11. 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
  12. 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';
  13. Read Operation Using http.get() method

  14. 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; }); } }
  15. Create Data Using http.post()

  16. 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();
  17. Update Data Using http.put()

  18. 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(); }); });
  19. Delete Data Using http.delete()

  20. 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(); }); }
  21. Stackblitz Demo https://stackblitz.com/github/NishuGoel/CRUDwithAngular Thank You!