1

Using javascript/typescript code, I'm trying to display a string (letter by letter) which works perfectly fine. But after displaying a string completely, it hides the element so bottom element takes that space which doesn't look good. I want that space to be there forever whether it contains string or not.

You can play with it here : https://plnkr.co/edit/mbZDrlOSI1vnjIrSMcNq?p=preview

@Component({
  selector: 'my-app',
  template: `
    <div>
      <div #text1></div>
      <div>Do you really think?</div>
    </div>
  `,
})
export class App {
  @ViewChild('text1') text1:ElementRef;

  ngAfterViewInit()
  {
    this.printLetterByLetter(this.text1, "Angular2 is awesome", 200)
  }

  printLetterByLetter(destination:ElementRef, message:string, speed:number){
    let i = 0;
    destination.nativeElement.innerHTML = "";
    let interval = setInterval(()=>{
      console.log(i);
        destination.nativeElement.innerHTML += message.charAt(i);
        i++;
        if (i > message.length){
            this.printLetterByLetter(this.text1, "Angular2 is awesome", 200)
            clearInterval(interval);
        }
    }, speed);
  }
}
1

2 Answers 2

3

Set a height for the element

<div #text1 [style.height.em]="1"></div>

Plunker example

Sign up to request clarification or add additional context in comments.

3 Comments

I ended up setting dynamic height: plnkr.co/edit/fM0C3BG5TuXTimsmxJoe?p=preview
The plunker link doesn't seem to contain the change. Did you safe the change?
Updated it now check.
1

A couple of ways that come to my mind is:

You can give a min-height style:

<div style="min-height: 18px" #text1></div>

Plunker: https://plnkr.co/edit/LR2m8TymNBeExwG0pDq2?p=preview

Or initialize the string with an empty string ""

2 Comments

I ended up setting dynamic height: plnkr.co/edit/fM0C3BG5TuXTimsmxJoe?p=preview
@micronyks nice :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.