【问题标题】:Handlebars.js: empty propetry when assigning to templateHandlebars.js:分配给模板时的空属性
【发布时间】:2018-09-24 16:12:56
【问题描述】:

当我登录时,它显示:

"names": {
    "en": "X"
},
"images": [
    "https://x.com/1.jpg"
]

模板是:

<div>{{images.[1]}}</div>                       
<div>{{names.en}}</div>

但是图片的div 是空的。

如何解决这个问题?

【问题讨论】:

  • images 是一个具有单个值的 数组,删除 .,并使用第一个索引:images[0]

标签: javascript html templates handlebars.js frontend


【解决方案1】:

试试:

<div>{{images.[0]}}</div>   

Handlebars 语法很奇怪,如果要访问数组中的元素,请执行以下操作:

{{images.[0]}}

这不起作用:

{{images[0]}}
{{images.0}}

但是,如果元素具有嵌套属性,您可以这样做:

{{images.0.otherProp}}

【讨论】:

    【解决方案2】:
    <div>{{images[0]}}</div>                       
    <div>{{names.en}}</div>
    

    images是一个数组,数组索引从0开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多