我使用了replace
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;
}
代码,如下所示。但它有一个问题。您能告诉我原因吗?
.TS
var occList = document.getElementById('occClass');
occArray = [];
for (var i = 0; i < occList.options.length; i++) {
if (occList.options[i].selected) {
console.log(occList.options[i].value)
occArray.push(occList.options[i].value);
}
}
console.log(occArray.length)
答案 0 :(得分:5)
根据Template Literals上的文档,将代码替换为:
let prompt = this.question.prompt.replace(`{{${this.answerCode}}}`,this.LookupAnswer(this.answerCode));
将"
替换为反引号`
,并将变量括在${ }
答案 1 :(得分:0)
请尝试使用字符串模板文字:
`${this.answerCode}`