Last active
May 27, 2020 19:25
-
-
Save AregSargsyan/a832122bcc6f94a9714f2a6e884b41ed to your computer and use it in GitHub Desktop.
Change Detection
This file contains 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, OnInit, ViewChild, ElementRef, NgZone, ChangeDetectorRef } from '@angular/core'; | |
@Component({ | |
selector: 'app-animated-div', | |
template: `<div #div></div>`, | |
styleUrls: ['./animated-div.component.scss'] | |
}) | |
export class AnimatedDivComponent implements OnInit { | |
@ViewChild('div', { static: true }) div: ElementRef | |
constructor(private ngZone: NgZone) { } | |
ngOnInit(): void { | |
this.changeColor(); | |
} | |
private setRandomColor() { | |
return ['red', 'orange', 'yellow', 'green', 'blue', 'purple'][Math.random() * 6 | 0]; | |
} | |
private changeColor() { | |
setInterval(_ => this.div.nativeElement.style.background = this.setRandomColor(), 50) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment