播放框架Twirl模板,比较javascript元素和scala变量 [英] Play framework Twirl template comparing javascript element and scala variable

查看:79
本文介绍了播放框架Twirl模板,比较javascript元素和scala变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下面的代码示例所示,我想比较scala helper元素内的javascript元素.但是,即使存在元素"abcde",它也始终返回false.除了使用标记外,如何在scala帮助器元素中获取javascript值?

As shown in the code sample below I want to compare a javascript element inside a scala helper element. However it always returns false even when the element "abcde" is present. How can I get the javascript value inside the scala helper element other than using tags?

@(appSeq:Seq(String))
<script>
var app = 'abcde'
@if(appSeq.contains(<script>app</script>)) {
   alert('true');
} else {
   alert('false');
}
</script>

推荐答案

由于这个原因,这是不可能的:

It is not possible for this reason:

  • var app = 'abcde'在JS VM中评估
  • XML文字<script>app</script>)在Java VM中评估
  • var app = 'abcde' is evaluated in the JS VM
  • the XML literal <script>app</script>) is evaluated in the Java VM

因此,由于它们是两个不同的上下文,因此您无法共享从JS到Scala的变量.您可以做的是将变量从Scala传递到JS,因为首先评估Scala代码,所有值都将呈现在模板中.

So since they are two different contexts you can't share a variable from JS to Scala. What you can do is pass a variable from Scala to JS, since Scala code is evaluated first and all the values will be rendered in the template.

您需要在Scala XML中对'abcde'进行硬编码,或者更好地将其分配给Scala值,例如:

You need either to hard code 'abcde' in the Scala XML or better assign it to a Scala value like:

@(appSeq:Seq(String))
@app="abcde"
<script>
var app = "@app"
@if(appSeq.contains(<script>$app</script>)) {
   alert('true');
} else {
   alert('false');
}
</script>

这篇关于播放框架Twirl模板,比较javascript元素和scala变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆