我有一个嵌套的 JSON 对象,如下所示
review:{
body:{
"1cfd0269-1b8f-418f-a7b2-45eb2fa7562b": "Text1",
"38d14dcd-6e70-46f9-8d81-9c5237cb7393": "Text2",
"01485828-39ef-4929-9e96-19758375eb9b": "Text3",
}
created_at: "2014-06-25T07:42:19Z",
id: "ea07aaa3-9db6-4868-b6f1-0887ef77f8ba",
product_id: "eb5a7c9c-c20d-4539-b04f-5a3fd8d26c87",
updated_at: "2014-06-25T07:42:19Z"
written_by: "09b3c6f1-cbcb-4544-8cc3-d073d17a8552",
written_on: "2014-06-25"
}
JS 非常简单:
var context = {review:review};
html = template(context);
模板较大,但相关部分在这里
<textarea class="review-body"> {{body["1cfd0269-1b8f-418f-a7b2-45eb2fa7562b"]}} </textarea>
我在尝试访问 html 模板中 body 的属性时遇到解析错误。有什么想法为什么会发生这种情况吗?
Uncaught Error: Parse error on line 5:
...iew-edit-context">{{body["1cfd0269-1b8f-418f-a7b2-45eb2fa7562b"]}}</tex
-----------------------^
Expecting 'ID', 'DATA', got 'INVALID'
最佳答案
摘自 Handlebars expressions 上的文档:
To reference a property that is not a valid identifier, you can use segment-literal notation, [ :
{{#each articles.[10].[#comments]}}
{{/each}}
这意味着您必须在模板中使用 {{body.[1cfd0269-1b8f-418f-a7b2-45eb2fa7562b]}}
。
关于javascript - 在使用 Handlebars.js 进行模板化时处理带有 UUID 键的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527316/