Hello I have a quick question about using HTTP service :
I would like to know if this is the right way to do.
Task
export interface Task { Title: string; AssignedTo: User; TaskStatus: string; } TaskService
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import 'rxjs/add/operator/map'; import { environment } from '../../environments/environment'; import { Task } from '../interfaces/task' @Injectable() export class TaskService { constructor(private http: HttpClient) { } getTask(id: number): Observable<Task> { const serviceUrl: string = environment.apiUrl + 'Tasks/' + id; return this.http.get(serviceUrl) .map((res: Task) => { return res; }); } } TaskComponent
export class TaskComponent implements OnInit { task: Task; constructor( private taskService: TaskService ) { } ngOnInit() { this.route.params.subscribe(params => { this.getTask(<number>params.id); }); } getTask(id: number) { this.taskService.getTask(id).subscribe( res => { this.task = res; console.log(this.task) }, err => { console.log(err); } ) } } template
<input type="text" class="form-control" id="title" placeholder="Title"[(ngModel)]="task.Title" name="title"> I'm not sure if that's the way to go now... I still have errors like: "ERROR TypeError: Cannot read property 'Title' of undefined", Except when I replace in my component "Task: Task" in "task: {}" ... Do I have to use an interface?