Angular 2 - 在模板中的变量内显示变量

AMa*_*z94 10 html javascript binding typescript angular

我在Angular 2中很新,我想在我的模板中显示一个变量中的字符串,该变量必须包含在另一个变量中.我将向您展示我的问题是什么以及我想要实现的目标的简化示例:


questionnaire.component.ts

/* Starts being "", populating an input text will modify this */
name = "Albert";

/* This variable comes from calling an API, here I just put it as created to simplify */
question.title = "Hello {{name}}, how old are you?";
Run Code Online (Sandbox Code Playgroud)

questionnaire.template.html

<p>{{question.title}}</p>
Run Code Online (Sandbox Code Playgroud)

我得到的结果是:

你好{{name}},你多大了?

我希望的结果是:

你好艾伯特,你多大了?

我试图逃避存储在我的数据库中的字符串中的"{{}}",使用ASCII字符而不是花括号,将其放在[innerHTML]中 ...但结果始终相同.

你知道我怎么解决这个问题?

非常感谢你!

Gün*_*uer 19

{{}} 仅适用于Angular组件模板,不适用于任意字符串,也不适用于动态添加到DOM中的HTML.

只需将其更改为

 question.title = `Hello ${this.name}, how old are you?`;
Run Code Online (Sandbox Code Playgroud)

使用TypeScript字符串插值.