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
  1. Create service and import it into app module and providers
  2. In service import Angular2TokenService and add it to your constructor
  3. Add a method that could look like so:
    getRestaurants() {
    this._tokenService.get(api-base + route-url).map(res => res.json())
    }
  4. Import service into component to display restaurants
  5. Add service to component’s constructor
  6. Add method in ngOnInit or constructor to make call to service getRestaurants method like so:
    allRestaurants() {
    this.restaurantService.getRestaurants().subscribe(data => this.restaurants = data)
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly