客户端javascript

时间:2016-07-18 12:45:45

标签: marko

我正在将数组json对象设置为数据变量,就像这个data.journeyDetail = detail;在marko文件中我需要在javascript块中访问它,并且每个记录需要从对象中打印FullName。怎么做?

服务器端代码:

 data.journeyDetail = detail;
 data.author = "najam";
 this.body = marko.load("./views/journeyDetail.marko").stream(data);

marko文件(客户端)

    <script>
    console.log("author=$data.journeyDetail.length");
    var length = $data.journeyDetail.length;
    var recs = $data.journeyDetail;
    console.log("len=", length);
    console.log("array=", array);
    for(var i=0; length; i++){
      console.log("counter=", recs[i].origin);
    }

   </script>

当页面呈现其给出的javascript错误时,有错误的行具有无效的赋值 var recs = [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object];     console.log(“len =”,length);

1 个答案:

答案 0 :(得分:1)

您希望使用JSON.stringify(...) 将模板数据序列化到页面。这将允许script中的代码访问呈现模板时首次可用的数据。请注意,您的data.journeyDetail数据不能包含循环引用,因为这会导致JSON.stringify(...)中出现错误。如果您在脚本代码运行时不需要所有信息,则可能需要使用Array map函数来转换数组中的数据。

您想要使用:

<script>
console.log("author=$data.journeyDetail.length");
var length = $data.journeyDetail.length;
var recs = ${JSON.stringify(data.journeyDetail)};
console.log("len=", length);
console.log("array=", array);
for(var i=0; length; i++){
  console.log("counter=", recs[i].origin);
}
</script>

唯一的变化是这一行:

var recs = ${JSON.stringify(data.journeyDetail)};