代码之家  ›  专栏  ›  技术社区  ›  Muhammad Ashraf

使用Mustache循环json

  •  0
  • Muhammad Ashraf  · 技术社区  · 6 年前

    我有这个:

      field: [
          { text: 'text 1' },
          { price1: '10' },
          { price2: '16' },
          { text: 'text2' },
          { price1: '20' },
          { price2: '23' }
        ];
    

    我想用胡子重复数据,我试过:

    {{#field}}
          <span>{{text}}</span>
    {{/field}}
    

    但没用,有人能帮忙吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Flash    6 年前

    当在moustach中迭代JSON时,您可以执行以下操作

    {{#each field}}
        <span> {{ text }} </span>
    {{/each}}
    

    顺便说一句,您的JSON没有正确呈现,我认为每个JSON对象必须看起来像这样

    field: [
        { 
            text: 'text 1,
            price1: '10',
            price2: '16'
        },
        { 
            text: 'text2',
            price1: '20',
            price2: '23'
        },
    ];