开发者

Play! templates - passing dynamic string to javascript doesn't work?

开发者 https://www.devze.com 2023-04-06 01:07 出处:网络
Basically, in the controller, something dynamic happens, and I pass a non-static string to the view: String token = \"\";

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}");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号