Search code examples
angularaudioonerror

how to access global variable inside onerror event of audio object


I want to access isDisabled variable from audio.onerror function and want to reset the value of isDisabled but unable to set it.

public isDisabled: boolean = false;
private verifyPrompt(url: string): boolean{
           let audio = new Audio(this.promptMessageUrl);
           this.isDisabled = 
           audio.load;
        audio.onerror = function(e){
            this.isDisabled = true;
        }
    }

Solution

  • Remove thefunction keyword and use arrow function notation ()=>.

    audio.onerror = (e) => {
        this.isDisabled = true;
    }