2017-05-02 36 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