【问题标题】:Meteor Conditionally displaying nested templatesMeteor 有条件地显示嵌套模板
【发布时间】:2015-04-02 06:16:53
【问题描述】:

我有一个包含几个子嵌套模板的模板,这些模板应该根据保存在 TemplateC 集合中的数据有条件地显示,如下所示,所以我在模板中使用了 if 条件,如下所示,但我总是显示所有子模板尽管条件返回真或假。有人可以检查我的代码并告诉我我在这里缺少什么吗?谢谢

        var templateArray = ['false', 'false'];

        Template.formBuilderPreview.created = function() {

            var cursor = TemplatesC.find({}, { sort: { templateCode: 1 }});    
            if (!cursor.count()) return;

            cursor.forEach(function (row) {
              //only case 1 return in the switch below as case 2 never exist
                switch(row.templateCode) {
                    case 1: templateArray[0] = true; break;
                    case 2: templateArray[1] = true; break;
                    default: templateArray[0] = true;
                }

            });
        };


        Template.formBuilderPreview.helpers({

            template1box: function(){      
                console.log(templateArray[0]);  //This returns true
                return templateArray[0];
            },
            template2box: function(){    
                console.log(templateArray[1]); //This returns false
                return templateArray[1];
            }

        });

模板:

    <template name="formBuilderPreview">

        <div id="fullpage">
            {{#if template1box}}                
                {{> temp01}}
            {{/if}}

            {{#if template2box}}                        
                {{> temp02}}
            {{/if}}            
        </div>

    </template>

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    您定义了一个字符串数组,我认为这是造成问题的原因,所以我建议您更改

    var templateArray = ['false', 'false'];
    

    var templateArray = [false, false];
    

    它会顺利运行

    【讨论】:

      【解决方案2】:

      把这些助手放在一起。

      Template.formBuilderPreview.helpers({
           template1box: function(){      
              if(templateArray[1]){
                   return true;
                }else{
                   return false;
                }
            });
      

      现在模板应该如下所示。

      <template name="formBuilderPreview">
           {{#if template1box}}  
           <!-- If helper return TRUE this temp01 will be showed. -->              
                  {{> temp01}}
              {{else}}
           <!-- If helper return FALSE this temp01 will be showed. --> 
                  {{> temp02}}
            {{/if}}
       </template>
      

      你得到了助手的想法,只在 1 个助手上实现,退休true/false

      【讨论】:

      • 感谢 Ethaan 的帮助,但如果我有两个以上的子模板,那就不实用了
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2014-11-22
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多