2017-05-02 39 views
0

我有下面的代码,我试图从选定的值创建一个字符串。为什么价值没有得到在打字稿中分配?

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

this.filter值是{category:${this.selectedCategory}}而我期待的输出作为{category:Music}

+0

其作为字符串处理插值。使用模板特定的引号。 – Nirus

回答

2

您是使用单引号”,但你应该使用',而不是为字符串插值:

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

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

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

您可以阅读更多关于它们的信息here