I am coding an API with Angular2
and NodeJS
, I am implementing services for my ِAPI that is supposed to get a list of tasks and display it. Here is the task service:
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class TaskService{
constructor(private http:Http){
console.log('Task Service Initialized...');
}
getTasks(){
return this.http.get('http://localhost:3000/api/tasks')
.map(res => res.json());
}
}
For my getTask
function (correct me if I am wrong) the .map()
function takes my response and formats it in an array of values.
Here is now, the task components that uses the task service:
import { Component } from '@angular/core';
import {TaskService} from '../../services/task.service';
@Component({
moduleId: module.id,
selector: 'tasks',
templateUrl: 'tasks.component.html',
})
export class TasksComponent {
constructor(private taskService:TaskService){
this.taskService.getTasks()
.subscribe(tasks =>{
console.log(tasks);
})
}
}
I would like to understand what this .subscribe()
function does and I can't find any relevant information.