Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法以编程方式引用把手内联部分?_Javascript_Typescript_Handlebars.js - Fatal编程技术网

Javascript 有没有办法以编程方式引用把手内联部分?

Javascript 有没有办法以编程方式引用把手内联部分?,javascript,typescript,handlebars.js,Javascript,Typescript,Handlebars.js,考虑下表template.hbs: <table class="table table-striped table-hover"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Value</th> </tr> <

考虑下表template.hbs:

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        {{#* inline "testTableRowTemplate"}}
        <tr>
            <td>{{Id}}</td>
            <td>{{Name}}</td>
            <td>{{Value}}</td>
        </tr>
        {{/inline}}
        {{#* inline "testTableEmptyTemplate"}}
        <tr>
            <td colspan="99">There are no rows</td>
        </tr>
        {{/inline}}
        {{#* inline "testTableLoaderTemplate"}}
        <tr>
            <td colspan="99">
                <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
                <span class="sr-only">Loading...</span>
            </td>
        </tr>
        {{/inline}}
    </tbody>
</table>


我希望避免表中有多个.hbs文件,因此理想情况下,我能够在同一个文件中定义可重用的分区。有什么想法吗?

您似乎无法从代码中引用这些想法。我的解决办法如下:

表格.hbs

您会注意到这里使用了一个定制的助手
registerPartial
。这允许我动态注册一个把手助手

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        {{#registerPartial "testTableRowTemplate"}}
        <tr>
            <td>{{Id}}</td>
            <td>{{Name}}</td>
            <td>{{Value}}</td>
        </tr>
        {{/registerPartial}}
    </tbody>
</table>
表.ts

通过
handlebar.compile(templateContentFromTablehbs)
加载Table.hbs模板后,我现在可以通过执行以下操作获得对行部分的编译引用:

this._rowTemplate = Handlebars.compile(`{{>${this._rowPartialName}}}`);
现在我可以重复使用
this.\u rowTemplate
对表行进行部分刷新,而不必刷新整个内容

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        {{#registerPartial "testTableRowTemplate"}}
        <tr>
            <td>{{Id}}</td>
            <td>{{Name}}</td>
            <td>{{Value}}</td>
        </tr>
        {{/registerPartial}}
    </tbody>
</table>
Handlebars.registerHelper('registerPartial', (name, options) => Handlebars.registerPartial(name, options.fn));
this._rowTemplate = Handlebars.compile(`{{>${this._rowPartialName}}}`);