2016-05-17 44 views
0

我已將[String, List[String]]的地圖傳遞給Play中的一個視圖。用戶正在從下拉列表中選擇一個值。這個值是我的地圖中的一個關鍵。我正在使用此密鑰,然後使用地圖中的值填充第二個下拉列表 - String的列表。但是,我不知道如何使用Play的模板語言從我的地圖中獲取值,因爲我正在通過JavaScript檢索密鑰。在Scala Play框架視圖中使用密鑰獲取地圖值

這是我的代碼:

function selectSubCat() { 
     var key = $("#monthYear").find(":selected").text(); // get the selected option's text. This is the map key. 

// Here lies the issue. mailingRunDates is the name of the map I've passed to the view. Play says: val key not found. 
     var runDatesList = @mailingRunDates(key) 

回答

1

我認爲你正試圖與playframework模板引擎通信的JavaScript。除了you are trying to communicate with a controller using ajax之外,我擔心你不能這樣做。

在你的情況下,編譯器正在尋找一個聲明爲dynamic statement的變量。它是在服務器中運行的代碼,所以當模板轉換爲html時,它不能被更改。你可以使用類似@{key=12}這樣的模板系統聲明一個變量。

因此,在這種情況下,我會將所有Map[String, List[String]]提取到javascript對象以在javascript中創建該功能。

相關問題