Sampath
Sampath

Reputation: 65870

String Interpolate is not working

I have used string replace code as shown below.But it has an issue.Can you please tell me why?

.ts

 answerCode: string = 'answer_28903220';

 constructor(){}

let prompt = this.question.prompt.replace("{{this.answerCode}}", 
this.LookupAnswer(this.answerCode));//Not working: Output= "{{answer_28903220}} was born on:"

let prompt = this.question.prompt.replace("{{answer_28903220}}", 
               this.LookupAnswer(this.answerCode));//This is working fine:output= Sampath was born on:

 LookupAnswer(answerCode: string): string {
    let answer: string = '';
    _.some(this.answers, (value, key) => {
      if (value.questionCode == answerCode.substring(7)) {
        answer = value.answer;
        return true;
      }
    });
    return answer;
  }

Upvotes: 1

Views: 632

Answers (2)

Jai
Jai

Reputation: 74738

Instead try with string template literals:

`${this.answerCode}`

Upvotes: 0

Christopher Moore
Christopher Moore

Reputation: 3099

As per the documentation on Template Literals, replace your code with this:

let prompt = this.question.prompt.replace(`{{${this.answerCode}}}`,this.LookupAnswer(this.answerCode));

Replace " with backticks ` and enclose the variable with ${ }

Upvotes: 5

Related Questions