找不到错误:值a

时间:2015-09-07 09:48:34

标签: javascript scala playframework

我定义了一个变量

@(mapCollectionCount: Map[String, String]) //values coming from controller
@{var a=0} 
<script type="text/javascript"> 
  var r=@mapCollectionCount.get("receipts"); 
  r=r.toLocaleString();
</script>
<div> @a<div> //gives me error not found: value a

1 个答案:

答案 0 :(得分:0)

您的代码不起作用的原因是,尽管在块中定义了var a,但该范围在您实际使用a之前完成。

使用defining

@(mapCollectionCount: Map[String, String]) //values coming from controller
@defining(0) { a => 
 <script type="text/javascript"> 
  var r=@mapCollectionCount.get("receipts"); 
  r=r.toLocaleString();
 </script>
 <div> @a<div> //shouldn't give error
}

同时检查这个答案:

Declare variable in a Play2 scala template