如何在车把的if条件内使用

时间:2019-03-22 12:03:26

标签: handlebars.js express-handlebars

{{#if docs}}
    {{#each docs}}
        <h4>customer number : {{this.customer}}</h4>
        <h4>number of liters required : {{this.volume}}</h4>
        {{#if alldri}}
            {{#each alldri}}
                <h5>{{this.name}}</h5>
            {{/each}}
        {{/if}}
    {{/each}}
{{/if}}

docs和alldri都是JSON数组.docs数组下的所有对象都可以正常打印,但是无法看到alldri数组下的对象。

alldri数组是

[ { _id: 5c94c61f955fa804fc96657c,
    name: '1',
    phonenumber: '9640121413',
    email: 'taditarun123@gmail.com'
    __v: 0 },
  { _id: 5c94c683955fa804fc96657d,
    name: '2',
    phonenumber: '9493447471',
    email: 't@gmail.com'
    __v: 0 } ]

docs数组是

[ { _id: 5c93a9812671d127785c105e,
customer: '1',
merchant: '11',
volume: '12',
__v: 0 } ]

1 个答案:

答案 0 :(得分:0)

您的问题实际上与if指令无关。有范围。在each指令中时,您将失去对父作用域的访问权限。因此,您将无法访问内部的alldri。这就是为什么它评估为false而不呈现该内容的原因。

请参见Access properties of the parent with a Handlebars 'each' loop

如果您的模板更改为:

,您的代码将可用
{{#if docs}}
    {{#each docs}}
        <h4>customer number : {{this.customer}}</h4>
        <h4>number of liters required : {{this.volume}}</h4>
        {{#if ../alldri}}
            {{#each ../alldri}}
                <h5>{{this.name}}</h5>
            {{/each}}
        {{/if}}
    {{/each}}
{{/if}}

请注意../alldri

https://jsfiddle.net/nowgy52m/7/

相关问题