Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.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_Handlebars.js - Fatal编程技术网

Javascript 在循环中引用把手变量

Javascript 在循环中引用把手变量,javascript,handlebars.js,Javascript,Handlebars.js,我试图使用编译的模板在Handlebar中构建一些下拉列表,但它无法访问我的变量: 车把: {{#each options}} <div class="control-group consumables-options"> <div class="row-fluid"> <div class="span2"> {{select 'task_services_options' ../bond_service_req

我试图使用编译的模板在Handlebar中构建一些下拉列表,但它无法访问我的变量:

车把:

{{#each options}}
<div class="control-group consumables-options">
    <div class="row-fluid">
        <div class="span2">
            {{select 'task_services_options' ../bond_service_request_quantities quantity}}
        </div>
    </div>
</div>
{{/each}}
其中bond_数量和opt是对象数组

select helper来自handlebar表单helpers,但即使在普通handlebar{{../bond\u service\u request\u quantities.0.text}中,我也会收到一个错误

未捕获的TypeError:无法读取未定义的属性“1”


你的把手模板和javascript代码看起来不错。从错误消息看,变量bond_数量似乎为空

这是一个用于测试代码的示例html文件。我试图尽可能地简化它,将预编译模板直接添加到页面,对数据进行硬编码

<html>

<head>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<script src="handlebars.form-helpers.min.js"></script>

<script>
    (function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['service_request_consumable_options'] = template({"1":function(depth0,helpers,partials,data,blockParams,depths) {

    depths = [];
    depths[1] = data.root;

    return "<div class=\"control-group consumables-options\">\n    <div class=\"row-fluid\">\n        <div class=\"span2\">\n            "
    + this.escapeExpression((helpers.select || (depth0 && depth0.select) || helpers.helperMissing).call(depth0,"task_services_options",(depths[1] != null ? depths[1].bond_service_request_quantities : depths[1]),(depth0 != null ? depth0.quantity : depth0),{"name":"select","hash":{},"data":data}))
    + "\n        </div>\n    </div>\n</div>\n";
},"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data,blockParams,depths) {
    var stack1;

  return ((stack1 = helpers.each.call(depth0,(depth0 != null ? depth0.options : depth0),{"name":"each","hash":{},"fn":this.program(1, data, 0, blockParams, depths),"inverse":this.noop,"data":data})) != null ? stack1 : "");
},"useData":true,"useDepths":true});
})();
</script>

<script>
    $(function() {
        HandlebarsFormHelpers.register(Handlebars);

        var bond_quantities = [ {
            value : 1,
            text : 'One'
        }, {
            value : 2,
            text : 'Two'
        } ];

        var opts = [ {
            quantity : 100
        }, {
            quantity : 200
        } ];

        var html = Handlebars.templates.service_request_consumable_options({
            bond_service_request_quantities: bond_quantities,
            options: opts
        });

        $('#target').html(html);
    });
</script>
</head>

<body>
    <div id="target"></div>
</body>

</html>

我的回答是,我使用了两种不同版本的把手。该项目有v2.0.0,但npm为编译模板的grunt任务安装了3.0.3。

否,如果我在循环外输出它,它工作正常,它就在那里。我使用Handlebar v2,这有关系吗?看起来在v2中深度参数没有传递到模板。您可以通过手动设置参数使模板工作。我将更新示例。
<html>

<head>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<script src="handlebars.form-helpers.min.js"></script>

<script>
    (function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['service_request_consumable_options'] = template({"1":function(depth0,helpers,partials,data,blockParams,depths) {

    depths = [];
    depths[1] = data.root;

    return "<div class=\"control-group consumables-options\">\n    <div class=\"row-fluid\">\n        <div class=\"span2\">\n            "
    + this.escapeExpression((helpers.select || (depth0 && depth0.select) || helpers.helperMissing).call(depth0,"task_services_options",(depths[1] != null ? depths[1].bond_service_request_quantities : depths[1]),(depth0 != null ? depth0.quantity : depth0),{"name":"select","hash":{},"data":data}))
    + "\n        </div>\n    </div>\n</div>\n";
},"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data,blockParams,depths) {
    var stack1;

  return ((stack1 = helpers.each.call(depth0,(depth0 != null ? depth0.options : depth0),{"name":"each","hash":{},"fn":this.program(1, data, 0, blockParams, depths),"inverse":this.noop,"data":data})) != null ? stack1 : "");
},"useData":true,"useDepths":true});
})();
</script>

<script>
    $(function() {
        HandlebarsFormHelpers.register(Handlebars);

        var bond_quantities = [ {
            value : 1,
            text : 'One'
        }, {
            value : 2,
            text : 'Two'
        } ];

        var opts = [ {
            quantity : 100
        }, {
            quantity : 200
        } ];

        var html = Handlebars.templates.service_request_consumable_options({
            bond_service_request_quantities: bond_quantities,
            options: opts
        });

        $('#target').html(html);
    });
</script>
</head>

<body>
    <div id="target"></div>
</body>

</html>