Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
How do I get my text from speech rendered to Angular Html?

I am trying to render a variable called "this.text" to my app.component.html. I am able to change "this.text" through a function called "recognition.onresult" but it doesn't render or change to the screen. The "recognition.onresult" function was orginally in the "speech.ts" file but I was getting better results with it in the component file.

app.component.html

<h1>{{text}}</h1>

app.component.ts

import { Component, OnInit, OnDestroy } from '@angular/core';
import { SocketService } from "./socket.service";
import recognition from './speech';

...

export class AppComponent implements OnInit, OnDestroy {

    text:any = 'hi';

      public constructor(private socket: SocketService) {
    }
    getText(text:any){
      console.log(text)
      this.text = text
     if (text === "what's today's date"){
       alert("none ya")
       this.text = "None ya"
     }
    }
    
    ngOnInit(){
      recognition.onresult = (event:any) => {
        // get the results of the speech recognition
        const resultIndex = event.resultIndex
        const result = event.results[resultIndex][0].transcript
        this.getText(result.toString())
        // perform actions based on transcript and level of confidence
      }
        this.socket.getEventListener().subscribe(event => {
          recognition.start();
            if(event.type == "message") {
                let data = event.data.content;
                if(event.data.sender) {
                    data = event.data.sender + ": " + data;
                }
            }
            if(event.type == "close") {
            }
            if(event.type == "open") {
                
              }
        });
    }
...
}

speech.ts

declare const webkitSpeechRecognition: any;

var SpeechRecognition = webkitSpeechRecognition;
let recognition = new SpeechRecognition()
recognition.lang = "en";
recognition.continuous = true

recognition.onstart = function () {
  // actions to be performed when speech recognition starts
  console.log(recognition)
};

recognition.onspeechend = function () {
  // stop speech recognition when the person stops talking
  recognition.stop();
}



export default recognition



about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If your logic is fine, then you could just use ChangeDetectorRef which will explicitly mark changes and re-render the view.

import { ChangeDetectorRef } from '@angular/core';
...

constructor(private cdr:ChangeDetectorRef) {
   ...
}

Use change detection in getText() function after updating text.

this.cdr.detectChanges();

This should reflect changes in HTML.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda