11. Create EventService
In this section we will add a service and discuss dependency injection.
1. Add an Angular service and return an observable of Attendee
Make an Angular service.
Run the following command to make a service.
ng g service event/services/event2. Add logic to the service
Add logic to the
EventServiceto return a hardcodedAttendeeobservable array.
import { Injectable } from '@angular/core';
import { Attendee } from '../../models';
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class EventService {
constructor() {}
getAttendees(): Observable<Attendee[]> {
return of([
{
name: 'Duncan',
attending: true,
guests: 0
}
] as Attendee[]);
}
}3. Inject service into the component
Inject and use the new service in the component.
Subscribe to the observable of Attendee returned from the service.
Add a getAttendees method to the component.
4. Swap from subscription to async pipe
Angular pipes, a way to write display-value transformations that you can declare in your HTML. The async pipe is a special built in pipe from Angular that will subscribe to an observable for you in the HTML template and also unsubscribe when the components ngOnDestory life cycle hook is fired. Another quirk is that it will also mark the component to be checked by Angular's change detection on its next cycle.
Use async pipe versus a subscription to get the attendees from the observable.
5. Use async pipe in HTML
Swap out attendee for
attendee$ | asyncin the HTML. We can leave the original attendees property alone for now.
6. Add fake backend
Angular's own HttpClientInMemoryWebApiModule can help us make a little mock server without needing to add a real server set up. You can read more about how it works here https://github.com/angular/in-memory-web-api.
Add fake backend with with npm.
Add
HttpClientModuleAdd the
HttpClientInMemoryWebApiModuleto the imports array of the@ngModuleand register theInMemoryDataServicewe will write in the next step.
7. Make the InMemoryDataService
Create the missing
InMemoryDataServicein the src/app folder. Do not worry too much about learning how this works we just need a little fake backend so we can focus on understanding Angular and NgRx.
8. Update Attendee interface to have an optional Id
Add optional id to Attendee interface with a
id?: numbersyntax.
9. Add HttpClientModule to EventModule
Angular modules describe the dependencies for this section of code so we will need to also add the HttpClientModule here to. The build tool is smart enough to know we registered it in the root module so we will not pay for it twice in the browser bundled JavaScript.
Add
HttpClientModuleto the EventModule.
10. Update service to call fake endpoint
Update the service by injecting the
httpClientinto the constructor.Change the
getAttendeemethod to use thehttpClientand fake backend.Add an
addAttendeemethod to save the added attendees.
12. Update container component to have an add attendee method
Add new method to call
addAttendeeon the service and then callgetAttendeesafter it saves.
StackBlitz Link
Last updated