Skip to content

Instantly share code, notes, and snippets.

@arturovt
Created June 6, 2019 23:02
Show Gist options
  • Save arturovt/27cc5292377a92266cd75dd03cf37229 to your computer and use it in GitHub Desktop.
Save arturovt/27cc5292377a92266cd75dd03cf37229 to your computer and use it in GitHub Desktop.
import { Component, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { finalize } from 'rxjs/operators';
@Component({
selector: 'app-todos',
template: `
<button (click)="loadTodos()">Load todos</button>
<app-todo *ngFor="let todo of todos" [todo]="todo"></app-todo>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TodosComponent {
public todos: Todo[] = [];
constructor(private ref: ChangeDetectorRef, private todoService: TodoService) {}
public loadTodos(): void {
this.todoService.getTodos().pipe(
finalize(() => this.ref.detectChanges())
).subscribe((todos) => {
this.todos = todos;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment