I've created a file input to open a video. And I'd like to display that video input at the condition that my_variable exists. This is what the HTML looks like:
<input type="file" (change)="handleFileInput($event)" accept="video/mp4">
<div *ngIf="my_variable">
<video id="video" width="200" height="200" src="{{ my_variable.path }}">
</video>
</div>
The (change) event is called whenever the user chooses a video. And my function handleFileInput is supposed to update my_variable like this:
handleFileInput(event: any) {
this.my_variable = event.target.files.item(0);
this.video = document.getElementById('video') as HTMLInputElement;
// do other stuff that requires this.video variable
}
My problem is that the this.video variable is null
. Actually, since the change event is not finished yet, the ng if
condition in the DOM does not update the view, and then, my video input is never created. Hence the fact that this.video is null
at this point. But I don't know how to solve this issue.
I didn't find anything on Stack Overflow, I tried to set a timeout when doing a document.getElementById('video')
. For example:
setTimeout(() => {
this.video = document.getElementById('video') as HTMLInputElement;
// do other stuff that requires this.video variable
}, 1000);
It actually worked, but I wish I could find a clearer solution. Could you please help me ? Thank you