0

I am trying to request permission from the user to record audio using AngularJs.

I have this method in my Component Class, but the class variables are not accessible using this.

public recordAnswer(event: any) {

    this.recording = true;
    this.toastr.error('Something went wrong. Try again.'); // this line works

    navigator.permissions.query({name: 'microphone'}).then(function (result) {

        console.log(result);

        if (result.state == 'granted') {
            // start recording audio
        } else if (result.state == 'prompt') {

        } else if (result.state == 'denied') {
            console.log(this.psp); // this line does not work
            this.toastr.error('Something went wrong. Try again.'); // this line does not work
        }

        result.onchange = function () {};
    });

}

I am getting this error:-

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'psp') TypeError: Cannot read properties of undefined (reading 'psp')

I had a look at this question, but I am not sure how I can access the class variables of MyComponent.

How can I access class variables so I can display a toastr.error to inform user to give permission?

Dula
  • 1,276
  • 5
  • 14
  • 23
  • For anyone looking for the answer for this one. `navigator.permissions.query({name: 'microphone'}).then(this.processPermissionResult.bind(this));` – Dula May 30 '23 at 00:28
  • I created a new function `public processPermissionResult(result : any) {}`. and transferred the contents to the new function and binded the `this` variable to the callback.(in the above comment) – Dula May 30 '23 at 00:31

0 Answers0