Providers Flashcards
1
Q
Say I have a Rails backend I want to make a request to, in order to fetch all restaurants, how could I receive this data in a component? Write answer in bullet points to make things easier.
A
- Create service and import it into app module and providers
- In service import Angular2TokenService and add it to your constructor
- Add a method that could look like so:
getRestaurants() {
this._tokenService.get(api-base + route-url).map(res => res.json())
} - Import service into component to display restaurants
- Add service to component’s constructor
- Add method in ngOnInit or constructor to make call to service getRestaurants method like so:
allRestaurants() {
this.restaurantService.getRestaurants().subscribe(data => this.restaurants = data)
}