Handlebars.js 跳过每个循环把手中的空值

Handlebars.js 跳过每个循环把手中的空值,handlebars.js,Handlebars.js,在车把中,我循环浏览一个列表,并根据以下值构建一个表: <table class="table table-striped table-condensed"> <thead> <tr> {{#each header}} <th data-id="{{@index}}" style="cursor: pointer">{{this.label}} <i class=

在车把中,我循环浏览一个列表,并根据以下值构建一个表:

<table class="table table-striped table-condensed">
    <thead>
        <tr>
            {{#each header}}
                <th data-id="{{@index}}" style="cursor: pointer">{{this.label}} <i class=" icon-resize-vertical"></i></th>
            {{/each}}
        </tr>
    </thead>
    <tbody>
        {{#each objects}}
            <tr>
                {{#each this.properties}}
                    <td>
                        {{#if @first}}
                            <a data-id="{{subIndex ../../this}}">{{this}}</a>
                        {{/if}}
                        {{#unless @first}}
                            {{#if this}}
                                {{this}}
                            {{/if}}
                        {{/unless}}
                    </td>
                {{/each}}
            </tr>
        {{/each}}
    </tbody>
</table>
本节的目的是首先检查循环中的当前值是否为null,如果不为null,则显示它。否则,跳过它。然而,这是一项重要的产出

[object Window]
好像
this
的值不知何故没有绑定到循环中的当前位置


我的问题基本上是,我是否需要以其他方式检查空值?

您找到解决方案了吗?我也遇到了同样的问题,解决方案是创建一个车把助手。第一次尝试时,我只是添加了一个条件来检查null。原来的值在编译时设置为window的实例,所以只需更改条件以检查window的实例

Handlebars.registerHelper('notNull', function(value, options) {
    if((value instanceof Window) == false) {
        return options.fn(this);
    }
    return options.inverse(this);
});
这适用于以下车把代码

{{#notNull this}}
    {{this}}
{{/notNull}}

你找到解决办法了吗?我也遇到了同样的问题,解决方案是创建一个车把助手。第一次尝试时,我只是添加了一个条件来检查null。原来的值在编译时设置为window的实例,所以只需更改条件以检查window的实例

Handlebars.registerHelper('notNull', function(value, options) {
    if((value instanceof Window) == false) {
        return options.fn(this);
    }
    return options.inverse(this);
});
这适用于以下车把代码

{{#notNull this}}
    {{this}}
{{/notNull}}

我使用了一个助手作为过滤函数。大概是这样的:

Handlebars.registerHelper({
    'notNull': function(value, options) {

    if(!Handlebars.Utils.isArray(value)){
        return [];
    } else {
        return value.filter(function(ele){
            return !Handlebars.Utils.isEmpty(ele);
        });
    }
}
车把代码:

{{#each (notNull elements)}}
    {{this}}
{{/each}}
结合上下文:

{elements:[{...},{...},{...},{...}]}
我不得不使用这个解决方案,因为有时读取数组可能包含空元素。在这种情况下,原始数组被缩减为非空元素

例如,在把手4.0.12中运行此功能:

{
    "elements": [{
            "unid": "1AB9BD4361CC9C2FC125832E00378BBF"
        }, {
            "unid": "4B8D99F9775B2E93C1258329002E8297"
        }, {
            "unid": "BFFA0EB7B3384AF7C1258328003758D0"
        }, {
            "unid": "7EDFA3EA1B588028C125832700378438"
        },
        null
    ]
}

{{#each (notNull elements)}}
    {{this.unid}}
{{/each}}
产生:

   1AB9BD4361CC9C2FC125832E00378BBF
   4B8D99F9775B2E93C1258329002E8297
   BFFA0EB7B3384AF7C1258328003758D0
   7EDFA3EA1B588028C125832700378438

我使用了一个助手作为过滤函数。大概是这样的:

Handlebars.registerHelper({
    'notNull': function(value, options) {

    if(!Handlebars.Utils.isArray(value)){
        return [];
    } else {
        return value.filter(function(ele){
            return !Handlebars.Utils.isEmpty(ele);
        });
    }
}
车把代码:

{{#each (notNull elements)}}
    {{this}}
{{/each}}
结合上下文:

{elements:[{...},{...},{...},{...}]}
我不得不使用这个解决方案,因为有时读取数组可能包含空元素。在这种情况下,原始数组被缩减为非空元素

例如,在把手4.0.12中运行此功能:

{
    "elements": [{
            "unid": "1AB9BD4361CC9C2FC125832E00378BBF"
        }, {
            "unid": "4B8D99F9775B2E93C1258329002E8297"
        }, {
            "unid": "BFFA0EB7B3384AF7C1258328003758D0"
        }, {
            "unid": "7EDFA3EA1B588028C125832700378438"
        },
        null
    ]
}

{{#each (notNull elements)}}
    {{this.unid}}
{{/each}}
产生:

   1AB9BD4361CC9C2FC125832E00378BBF
   4B8D99F9775B2E93C1258329002E8297
   BFFA0EB7B3384AF7C1258328003758D0
   7EDFA3EA1B588028C125832700378438
除非@first}在做什么?内部if语句可以工作,我有一个基本的例子,在这里,
{{{{除非@first}}
语句只在它不是循环中的第一个索引时运行,
{{除非@first}
在做什么?内部if语句有效,我这里有一个基本的例子,
{{{#除非@first}}
语句只有在它不是循环中的第一个索引时才会运行