Skip to Main Content
Mastering Angular Components - Second Edition
book

Mastering Angular Components - Second Edition

by Gion Kunz
July 2018
Intermediate to advanced content levelIntermediate to advanced
402 pages
9h 35m
English
Packt Publishing
Content preview from Mastering Angular Components - Second Edition

Loading data in the task service

It's time to change our task service and make use of the Angular HTTP client to obtain the task data from our database. Let's open up the src/app/tasks/task.service.ts file and change the file content to the following:

import {Injectable} from '@angular/core';import {HttpClient} from '@angular/common/http';import {BehaviorSubject} from 'rxjs';import {Task} from '../model';@Injectable()export class TaskService {  private tasks = new BehaviorSubject<Task[]>([]);  constructor(private http: HttpClient) {    this.loadTasks();  }  private loadTasks() {    this.http.get<Task[]>('/api/tasks')      .subscribe((tasks) => this.tasks.next(tasks));  }  getTasks() {    return this.tasks.asObservable();  }  addTask(task: Task) { return this.http ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Angular, 2nd Edition

Learning Angular, 2nd Edition

Brad Dayley, Brendan Dayley, Caleb Dayley
Essential Angular

Essential Angular

Victor Savkin, Jeff Cross
Pro Angular 6

Pro Angular 6

Adam Freeman

Publisher Resources

ISBN: 9781788293532Supplemental Content