为什么没有在打字稿中分配值?

时间:2017-05-02 12:14:28

标签: angular typescript

我有以下代码,我试图从所选值创建一个字符串。

this.selectedCategory = selectedvalue.name;
this.filter = '{category:${this.selectedCategory}}';

this.filter的值为 {category:${this.selectedCategory}} ,而我希望输出为 {category:Music}

2 个答案:

答案 0 :(得分:2)

您需要使用`而不是'用于模板文字。否则它只是一个简单的字符串。

this.filter = `{category:${this.selectedCategory}}`;

您可以阅读有关他们的更多信息here

答案 1 :(得分:2)

您正在使用单引号',但您应该使用`代替字符串插值:

this.selectedCategory = selectedvalue.name;
this.filter = `{category:${this.selectedCategory}}`;
相关问题