1

I have a string like:

How are __ doing today ?. I'm doing __.

I wanted to replace every __ by a text box. So the purpose is '__' is indicated as a fill in the blanks in the question.

2
  • str.replace(/__/g, '<input type="text" />') Commented Apr 21, 2018 at 5:56
  • Is this really related to angular? Commented Apr 21, 2018 at 6:02

1 Answer 1

2

If you want to use the Angular templates, you can split the string into an array, like so:

sentence = "How are __ doing today ?. I'm doing __.";
parts = this.sentence.split("__");

Then, in the template, loop through the parts, and add an input for all but the last:

<span *ngFor="let part of parts; let last = last">
  {{part}}
  <input type="text" *ngIf="!last"/>  
</span>

Here is a StackBlitz to show it working: https://stackblitz.com/edit/angular-eau2id?file=app%2Fapp.component.html

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

1 Comment

you understood my problem exactly. There is little change I'm having string at view not at component side. Something Like: <h4 [innerHTML]="replaceFillBlanks(item.question)"></h4> Component Part: replaceFillBlanks(question){ this.parts = question.split("__"); console.log(this.parts); }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.