当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Can't get data with Handlebars from JSON

问题描述:

Here is fiddle with code: jsfiddle

{

"status": {

"success": true,

"error": ""

},

"data": {

"item": [

{

"name": "Test 1",

"keywords": [

"[asdad,dasdas]"

],

"description": "xxzfrfa",

"test": 12.3,

"id": "584ab61344be2bd806ef276d"

},

{

"name": "My test 2",

"keywords": [

"[zx,zz]"

],

"description": "xz",

"test": 53,

"id": "584ab67b11b75c3a10fc1518"

}

]

}

}

...

<div id="product-container"></div>

<script id="product-template" type="text/x-handlebars-template">

{{#each data.item}}

<h2>{{brand}}</h2>

{{/each}}

</script>

What can I do to return data from JSON ?..it's not working now.. If anyone can help me. I've tried a lot of things.

网友答案:

The code you provided is working, there is nothing to render since there is no brand field.

Here is working example: https://jsfiddle.net/panamaprophet/nbn39wzu/

(since i didnt found a brand field in your example, i replaced it with name just for demonstration).

网友答案:

You can access the name of an item like below

{{#data.item}} 
    <h2>{{name}}</h2>
{{/data.item}}

Sample code below: (Removed ajax to get it working here)

Fiddle

分享给朋友:
您可能感兴趣的文章:
随机阅读: