Created
September 12, 2016 11:54
-
-
Save Shuumatsu/836024d8bc41318516d4d4f6a666afc6 to your computer and use it in GitHub Desktop.
Triggers the next round of Angular change detection after one turn of the browser event loop ensuring display of msg added in onDestroy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
import { Component, Input, OnDestroy, OnInit } from '@angular/core'; | |
let nextId = 1; | |
@Component({ | |
selector: 'heavy-loader', | |
template: '<span>heavy loader #{{id}} on duty!</span>' | |
}) | |
export class HeavyLoaderComponent implements OnDestroy, OnInit { | |
id = nextId++; | |
@Input() logs: string[]; | |
ngOnInit() { | |
// Mock todo: get 10,000 rows of data from the server | |
this.log(`heavy-loader ${this.id} initialized, | |
loading 10,000 rows of data from the server`); | |
} | |
ngOnDestroy() { | |
// Mock todo: clean-up | |
this.log(`heavy-loader ${this.id} destroyed, cleaning up`); | |
} | |
private log(msg: string) { | |
this.logs.push(msg); | |
this.tick(); | |
} | |
// Triggers the next round of Angular change detection | |
// after one turn of the browser event loop | |
// ensuring display of msg added in onDestroy | |
private tick() { setTimeout(() => { }, 0); } | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment