简体   繁体   中英

Assign a JavaScript var to a Thymelaf var

Is there a way to assign a JavaScript var to a Thymeleaf var? I got some code like this:

<script th:inline="javascript">
/*<![CDATA[*/
var someVar= /*[[${thymeleafObject.someValue}]]*/ 'default value';
/*]]>*/
<script>

When I change someVar (eg someVar+=1 ), is there a way to assign this value back to the Thymeleaf variable?

I thought about something like:

/*<![CDATA[*/
[[${thymeleafObject.someValue}]] = someVar;
/*]]>*/

but that didn't work.

There is no such thing as a Thymeleaf variable. There is a special syntax in Thymeleaf that allows for assigning variables from the template engine. In the rendered document the variable someVar will just be a plain javascript variable which can be changed as usual. You will be able to just write someVar += 1 .

If you want to pass the variable back to the server you cannot just assign it like in your second example but you have to make a request including the data to the server either via a html form or ajax. You can then accept the data in your controller and process it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM