O'Reilly logo

Developing an Angular 2 Edge by Ran Wahle, Mary Gualtieri, Wojciech Kwiatek, Suguru Inatomi, Filip Lauc, Troy Mott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Services

We had a brief introduction to services in Chapter 3. We explained that in Angular 2, application services should handle sending and receiving data, allowing components to focus on controlling the view.

In Chapter 3 we also created the UserService to handle fetching a list of users for the UserBlockComponent. However, the UserService will only return a “hard coded” list of users, not really communicating with the server to get the data.

import {Injectable} from '@angular/core';

@Injectable() 
export class UserService {

    private users = [
        {id: 1, username: 'filip.lauc93@gmail.com', status: 'online'},
        {id: 2, username: 'laco0416@gmail.com', status: 'offline'},
        {id: 3, username: 'mgualtieri7@gmail.com', status: 'online'},
        {

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required