you need Observable self managed and dedicated for task list lifecycle.
Attached example with BehaviorSuject and TaskService. For this example, instead to load from ajax request, i just populate dummy data after 1 secs.
Then you have update / delete action who both have to update list according to action done by user.
Component.ts :
import {
Component, OnInit
} from '@angular/core';
import {TaskModel, TaskService} from './services/task.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(public taskService: TaskService) { }
ngOnInit() {
this.taskService.fetchTask();
}
onChangeHandler(task: TaskModel)
{
this.taskService.save(task);
}
onDeleteHandler(task: TaskModel) {
this.taskService.remove(task);
}
}
Here we just manage view and ask service according to action (or lifecycle hook). On init we want to load from server, then if checkbox is changing, we want to update our reference, when we click on delete button, we want to remove from list (and may on server as well).
component.html
<h2>TODO LIST</h2>
<div *ngFor="let task of (taskService.task$ | async)">
<p>
{{ task.title }} | <input type="checkbox" [(ngModel)]="task.status" (change)="onChangeHandler(task)"> | <button (click)="onDeleteHandler(task)"> Delete </button>
</p>
</div>
now main code is on task.service :
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import {isUndefined} from 'util';
export interface TaskModel {
id: number;
title: string;
status: boolean;
}
@Injectable()
export class TaskService {
/**
* Observable who should always be the replica of last tasks state.
*/
private _tasks$: BehaviorSubject<TaskModel[]>;
/**
* array of task, use for each action done on task.
*/
private tasks: TaskModel[];
constructor() {
// We init by empty array.
this._tasks$ = new BehaviorSubject([]);
this.tasks = [];
}
/**
* Fake fetch data from server.
*/
fetchTask()
{
// Fake request.
Observable.create(obs => {
/**
* After 1 secs, we update internal array and observable.
*/
setTimeout(() => {
this.tasks = this.getDummyData();
obs.next(this.tasks);
}, 1000);
}).subscribe(state => this._tasks$.next(state));
}
/**
* Magic getter
* @return {Observable<{id: number; title: string; status: boolean}[]>}
*/
get task$(): Observable<TaskModel[]> {
// return only observable, don't put public your BehaviorSubject
return this._tasks$.asObservable();
}
/**
* We update from internal array reference, and we next fresh data with our observable.
* @param {TaskModel} task
*/
save(task: TaskModel) {
const index = this.tasks.findIndex(item => item.id === task.id);
if(!isUndefined(this.tasks[index]))
{
this.tasks[index] = task;
}
// Notify rest of application.
this._tasks$.next(this.tasks);
}
/**
* We remove from internal array reference, and we next data with our observable.
* @param {TaskModel} task
*/
remove(task: TaskModel) {
this.tasks = this.tasks.filter(item => item.id !== task.id);
this._tasks$.next(this.tasks);
}
/**
* Fake data.
* @return {{id: number; title: string; status: boolean}[]}
*/
private getDummyData() : TaskModel[]
{
return [
{
id: 1,
title: 'task1',
status: true
},
{
id: 2,
title: 'task2',
status: false
},
{
id: 3,
title: 'task3',
status: true
}
];
}
}
Online code
this.dataService.loadTasks(someurl)?HttpClientrequest. Updated my post.