Exploring the capabilities of Angular 4 with the integration of the Web

Trying to integrate the Web Speech API Interfaces (https://github.com/mdn/web-speech-api/) with an Angular application (version 4.25) and an ASP Core web server. The project is built using Visual Studio 2017 (version 15.7.1). Added @types/webspeechapi type definitions to the package.json file (version 0.0.29).

The Angular component code snippet appears as follows:

 /// <reference path="../../../../node_modules/@types/webspeechapi/index.d.ts" />
    import { Component } from '@angular/core';

    @Component({
        selector: 'home',
        templateUrl: './home.component.html'
    })
    export class HomeComponent {

        constructor() {

            var recognition = new SpeechRecognition();
            var speechRecognitionList = new SpeechGrammarList();
...

No compilation errors are shown at design time. However, attempting to open the page results in a 500 error message NodeInvocationException: Uncaught (in promise): ReferenceError: SpeechRecognition is not defined

The console log of the web server within Visual Studio displays the same error message.

The goal was to utilize the typing definitions but there seems to be an issue with resolving the type SpeechRecognition.

Answer №1

Jonathan's hint was spot on - while Chrome does support this feature, it requires the use of webkit prefixes.

So, I decided to add a check:

var reg = SpeechRecognition;
var typ = typeof SpeechRecognition;

reg = typ === "undefined" ? new webkitSpeechRecognition() :
                                new SpeechRecognition();

This solution is now compatible with both Firefox and Chrome browsers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmitting occasional feedback from ASP.NET Web API function to jQuery AJAX

I am currently working on a project that requires sending intermittent status responses from a Web API method back to a jQuery AJAX call in order to display progress in the UI. The process involves the jQuery AJAX calling the Web API method with specific ...

Making calls to an Angular GRPC API through an Envoy proxy

Having trouble connecting to the GRPC server from the UI via Envoy. Here's the code snippet for the Envoy proxy: static_resources: listeners: - address: socket_address: address: 0.0.0.0 port_value: 8888 filter_chains: ...

How to use TypeScript to set a value in ng2-ckeditor

I have implemented two ckeditor instances using *ngFor: <div class="form-group" *ngFor="let lang of languages"> <span>Legal text in {{lang.translate}} {{lang.abbr}}</span> <ckeditor id="{{lang.abbr}}" formControlName="{{lang.abbr} ...

How can I make the snackbar open multiple times in a row?

Check out this codesandbox I created to show an issue. When you click the button, a MUI snackbar opens. However, if you close it and try to reopen it, nothing happens. Do you think the problem is related to how I'm using hooks? Explore the sandbox h ...

Guide on setting up next-auth for external API login in a Next.js 13 app directory

Hey there, I'm currently working on setting up a login system with next-auth in Next.js 13 using appDir: true. The version of Next.js I am using is 13.3.0, but after going through the documentation, I must admit that I'm struggling to fully grasp ...

Is there a way to convert a literal type from the type level to the term level in TypeScript?

Consider this scenario: I have created a type that can only hold one specific value: export type IfEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends ...

Is it possible to use non-numeric values as keys in a Typescript Map?

During a lesson: private items: Map<number, string> = new Map(); ... this.items[aNumber] = "hello"; Results in this error message: An element has an any type because the expression of type number cannot be used to index type Map<numbe ...

Tips for choosing a subset of objects within a larger array of objects and generating a new array from them?

I am working with an array of 3586 objects, each containing various products. How can I create and store a new array that only includes the products from a specific index? console.log listarPorReferencia() { this.linxService.listarPorReferencia().subs ...

Angular10 selection table - reveal expanded row when item is selected

I am currently working with an angular 10 table that includes a selection feature, but I am encountering issues trying to display an expandable row when the user selects a particular row. I have attempted to incorporate expandable tables in conjunction wit ...

What is the reason behind Jest's failure with the error message "component is a part of the declaration of 2 modules"?

While running a test in an Angular (NX) project, I encountered the following exception: Error: Type FooComponent is part of the declarations of 2 modules: FooComponentModule and FooComponentModule! Please consider moving FooComponent to a higher module tha ...

typescript makeStyles() functions from material-ui library

I've been struggling to find the correct type without relying on any. I have a working code that styles the component as expected: import { makeStyles } from '@material-ui/core/styles' const useStyles = makeStyles((theme) => ({ mainC ...

When applying animations to ngIf, the elements end up overlapping

I'm struggling to animate div elements when toggled using the Angular directive *ngIf. The issue I'm facing seems to be a common delay/timing problem in animations, but I haven't been able to find a solid solution. 1) When the current elem ...

What methods can I use to ensure that both variables are true and maintain their relationship in a logical 'and' statement?

The code snippet provided raises a question about the logic behind the declaration of variable x as a string: function sample(one: boolean, two?: string) { if (one || two) { const x: string = one ? 'hello' : two; // Type 'string | ...

What could be the reason for the absence of a TypeScript error in this situation?

Why is it that the code below (inside an arbitrary Class) does not show a TypeScript error in VSCode as expected? protected someMethod (someArg?: boolean) { this.doSomething(someArg) } protected doSomething (mustBePassedBoolean: boolean) { /* ... * ...

Encountering difficulty retrieving host component within a directive while working with Angular 12

After upgrading our project from Angular 8 to Angular 12, I've been facing an issue with accessing the host component reference in the directive. Here is the original Angular 8 directive code: export class CardNumberMaskingDirective implements OnInit ...

Tips on implementing conditional validators in Angular's formBuilder

Is it possible in Angular 7.x to use formBuilder and ReactiveForms to apply a validator to a form based on the user's role? For instance, if the user has a specific role, they would be required to input certain fields. The user information is stored i ...

Module not found

Hey everyone, I recently updated my project to node version v14.18.0, but now I'm encountering a "module not found" issue (see screenshot below). Any suggestions on how to resolve this? https://i.stack.imgur.com/k0u82.png ...

Encountering an XHR error when using a systemjs module in TypeScript

Error: GET http://localhost:63342/Dog.js 404 (Not Found) XHR error (404 Not Found) loading http://localhost:63342/Dog.js <br/><br/>Below is the script in my index.html file. ...

The random number generator in TypeScript not functioning as expected

I have a simple question that I can't seem to find the answer to because I struggle with math. I have a formula for generating a random number. numRandomed: any; constructor(public navCtrl: NavController, public navParams: NavParams) { } p ...

Converting JavaScript object data to x-www-form-urlencoded: A step-by-step guide

I am trying to convert a JavaScript object into x-www-form-urlencoded. Is there a way to achieve this using Angular 2? export class Compentency { competencies : number[]; } postData() { let array = [1, 2, 3]; this.comp.competencies ...