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
ng g service event/services/event2. Add logic to the service
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
4. Swap from subscription to async pipe
5. Use async pipe in HTML
6. Add fake backend
7. Make the InMemoryDataService
8. Update Attendee interface to have an optional Id
9. Add HttpClientModule to EventModule
10. Update service to call fake endpoint
12. Update container component to have an add attendee method
StackBlitz Link
Last updated