How to change HTML via Typescript (Angular 2)?

Asked

Viewed 545 times

0

I am trying to create a "typewriter" effect where one letter is inserted after another. The code is like this:

Typescript:

TypeWriter() {
    let element = <HTMLInputElement>document.getElementById("nome");
    let frase = "Oi tudo bem?";
    let soletra = frase.split('');
    for(let i = 0; i < soletra.length; i++) {
      setTimeout(() => element.innerHTML += soletra[i], 75 * i);
   }
  }

HTML:

<h3 id="nome"> Split: </h3>

The problem is it’s not working. The idea is that each letter of the phrase "Hello good morning" is inserted into html (element.innerHTML) every 75 milliseconds (the multiplication serves to ensure that the letters are not inserted at the same time, so the letter of vector 1 will be inserted 75 milliseconds + the position (75 + 1) after the letter in vector 0)

// UPDATE - Version without Rxjs //

Typescript:

  export class Componente1Component implements OnInit {
        frase = '';
        ngOnInit() {

          const soletra = 'Oi tudo bem?'.split('').reverse();
          const soletrar = (a) => {
            this.frase += a;
            if (soletra.length > 0) {
              setTimeout(() => soletrar(soletra.pop()), 75)
            }
          }; 
}

HTML: I have not changed

<h3 id="nome"> {{ frase }} </h3>
  • As I mentioned before in another question asked by you, when you do ...innerHTML = "letter, 75 * position") vc is simply inserted in the element the text letter, 75 * position, to perform the calculation use template string, what I don’t know if it will guarantee that it will work, because it seems to me that the code is not correct!

  • Leandrade corrected the for. If I do something like " <H3 id="name"> Split: {{ Typewriter() }} </H3>) " in HTML, innerHTML works, but in a disorderly and infinite way

1 answer

3


Weslipe, I made some adjustments using a bit of Rxjs. See below the code:

Template

<div>{{frase}}</div>

Class

import { Component, OnInit } from '@angular/core';
import { from, of } from 'rxjs';
import { delay, concatMap } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  frase = '';
  ngOnInit() {
     const soletra = 'Oi tudo bem?'.split('');
     from(soletra)
      .pipe(
        concatMap(x => of(x).pipe(delay(75)))
      )
      .subscribe(a => {
       this.frase += a;
      });
   }
}

Resultado da execução do código acima

  • man, it worked! Thank you very much. I will have to study about Rxjs pq did not understand anything from your code kk, thank you for the collaboration!

  • 1

    @Weslipe this excerpt could be written without Rxjs. It would look like this: const soletra = 'Oi tudo bem?'.split('').reverse(); const soletrar = (a) => { this.frase += a; if (soletra.length > 0) { setTimeout(() => soletrar(soletra.pop()), 75) } };

  • Thanks for the comment. I tried to reproduce your version without Rxjs and it didn’t work, if possible, could you tell me what I did wrong? I added in the publication :)

  • @Weslipe all right? Add soletrar(soletra.pop()); in the last line of your method ngOnInit, in the example you put in the post. I believe only lacked the "start" of the function soletrar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.