如何从handlebarsjs访问这个json对象 [英] How to access this json object from handlebarsjs

查看:114
本文介绍了如何从handlebarsjs访问这个json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从handlebarsjs访问这个json对象

  [{
id:9,
name:Name1,
address:address1,
city:city1,
state:KS,
邮编:11111,
country:USA,
fax:111111,
电话:1111111,
:,
账户:11111,
contacts:[]
},{
id:12,
name :Name2,
address:address2,
city:city2,
state:NJ,
zip: 11111,
country:USA,
fax:,
phone:1111,
website:,
account:11111,
contacts:[{
firstName:name,
lastName:lastname,
title:rep,
phone:3333,
email:33333
}]
}]

我尝试使用{{name}}访问名称,但是没有工作,那么我如何访问姓名属性和联系人下的嵌套firstName属性?



谢谢

解决方案

您很幸运:您的问题在Handlebars文档页面中有相当清楚的解释:

http://handlebarsjs.com/



只需向下滚动到Handlebars路径部分,并且您会看到它正在讨论您正在查找的内容(Handlebars也支持嵌套路径,使查找嵌套在当前上下文之下的属性成为可能......)。


How to access this json object from handlebarsjs

    [ {
    "id" : 9,
    "name" : "Name1",
    "address" : "address1",
    "city" : "city1",
    "state" : "KS",
    "zip" : "11111",
    "country" : "USA",
    "fax" : "111111",
    "phone" : "1111111",
    "website" : "",
    "account" : "11111",
    "contacts" : []
}, {
    "id" : 12,
    "name" : "Name2",
    "address" : "address2",
    "city" : "city2",
    "state" : "NJ",
    "zip" : "11111",
    "country" : "USA",
    "fax" : "",
    "phone" : "1111",
    "website" : "",
    "account" : "11111",
    "contacts" : [ {
        "firstName" : "name",
        "lastName" : "lastname",
        "title" : "rep",
        "phone" : "3333",
        "email" : "33333"
    } ]
} ]

I have tried {{name}} to access name but that didn't work, so how do i access name attribute, and the nested firstName attribute under contacts?

Thank you

解决方案

You're in luck: your question is explained pretty clearly on the Handlebars documentation page:

http://handlebarsjs.com/

Just scroll down to the "Handlebars Paths" section, and you'll see it talks about exactly what you're looking for ("Handlebars also supports nested paths, making it possible to look up properties nested below the current context ...")

这篇关于如何从handlebarsjs访问这个json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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