2015-04-24 35 views
2

我有這樣的代碼:Thymeleaf:重新分配一個局部變量

<div th:each="element : ${list}" th:with="test=false"> 
    <div th:each="element2 : ${list2}"> 
     <div th:if="element2.name == 'someName'"> 
      <div th:with="test=true">test changed</div> 
     </div> 
    </div> 
    <div th:text="${test}"></div> 
</div> 

但是變量「測試」始終是始終爲false。我能做什麼?

+0

可能重複的[Thymeleaf變量變量](http://stackoverflow.com/questions/22597604/thymeleaf-change-variable) –

回答

1

這是因爲在th:with中定義的變量僅在包含<div>標籤的範圍內可用。這在使用Thymeleaf指南局部變量section中進行了說明。

使用th:with無法全局更新本地變量。

+0

我知道...有沒有另一種方式(沒有:與)達到相同結果? –

+0

我認爲沒有辦法只使用Thymeleaf,爲了實現這樣的事情也許可以使用JavaScript或使用定義的模型屬性,檢查:http://forum.thymeleaf.org/Simple-Counter-with-defined-variable- td4026976.html –