Play! templates - passing dynamic string to javascript doesn't work?
Basically, in the controller, something dynamic happens, and I pass a non-static string to the view:
String token = "";
render(token);
I can easily do:
<div id="token>${token}</div>
...and grab the contents:
$('#token').html();
But what doesn't seem to work is, in the javascript, doing:
function token(token) {
// do someth开发者_如何转开发ing with token
console.log('token: ' + token);
}
token(${token});
I can kind of see why it doesn't work...but what is the syntax for this?
You didn't name your function. It should be:
function token(token) {
// do something with token
console.log('token: ' + token);
}
token(${token});
Edit: You may need to just add quotes:
token("${token}");
By the way, this works, but in general I'd avoid using the same name for the function and its argument. A better name might be logToken
:
function logToken(token) {
// do something with token
console.log('token: ' + token);
}
logToken("${token}");
精彩评论