Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. Nishu Goel Software Engineer, IBM | Udemy Author | Angular

    Developer @DcoustaWilson Blog: https://nishugoel.wordpress.com HELLO!
  2. 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/
  3. Three ways - Return data from local File - Use

    local JSON file - Use Angular-in-memory-web-api
  4. AGENDA ❑ Getting the data from the in memory data

    store ❑ Reading this data ❑ Creating the data ❑ Updating the data ❑ Deleting the data
  5. ❑ 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) ] )
  6. 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
  7. 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
  8. 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';
  9. 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; }); } }
  10. 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();
  11. 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(); }); });
  12. 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(); }); }