I'm trying to add a class to an element and remove it after a certain time using setTimeout
.
Component
export class setClass implements OnInit {
className: string = ''
setClassName(newClass) {
this.className = newClass
setTimeout(function () {
this.className = ''
}, 1500)
}
constructor() { }
ngOnInit() {
}
}
Template
<div>
<img src="img/1.png" alt="" [ngClass]="className">
<button (click)="setClassName('foobar')">Set new class</button>
</div>
I can see the function adds the class name 'foobar' but it stays forever. I'm expecting this function to remove the newly added after 1500ms.
Please help me fix this.