Skip to content

Instantly share code, notes, and snippets.

@AregSargsyan
Last active May 27, 2020 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AregSargsyan/a832122bcc6f94a9714f2a6e884b41ed to your computer and use it in GitHub Desktop.
Save AregSargsyan/a832122bcc6f94a9714f2a6e884b41ed to your computer and use it in GitHub Desktop.
Change Detection
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