【问题标题】:Ember js: accessing variables within a {{view}} helperEmber js:访问 {{view}} 助手中的变量
【发布时间】:2013-08-07 00:22:30
【问题描述】:

我有以下模板,我在其中循环对象列表并希望有一个复选框,该复选框绑定到该对象的 isChecked 字段。这需要在视图助手中才能使 for 标签工作(我认为)。当我这样做时,我似乎无法弄清楚如何保持与 isChecked 字段的绑定。

{{#each listEntry in listEntries}}
    {{#view}}
        {{view Ember.Checkbox viewName="checkboxView" checkedBinding="listEntry.isChecked"}}
        <label {{bindAttr for="view.checkboxView.elementId"}}>Option 1</label>
    {{/view}}
{{/each}}

【问题讨论】:

    标签: ember.js handlebars.js


    【解决方案1】:

    您的问题类似于that,但这种方法不起作用,我认为是因为each 助手。

    但是one 的 cmets 表示将您的组件嵌套在标签中。 我已经这样做了。

    {{#each listEntry in listEntries}}
          <label>          
              {{view Ember.Checkbox viewName="checkboxView" checkedBinding="listEntry.isChecked"}}          
              Option 1
         </label>    
      {{/each}}
    

    我创建了一个jsfiddle 显示

    【讨论】:

    • 非常感谢您的帮助。你有什么帮助,但不是我想要的。这主要是因为我的问题中没有包含足够的上下文。我发布了我必须做些什么来解决它的答案。不过必须有更好的方法。
    • 没问题@DSJ。至少这里有两种方法可以解决这个常见问题:)
    【解决方案2】:

    这就是我最终要做的。我一直遇到的问题是“for”属性的绑定需要与检查绑定不结合使用。事情超出了范围。如果有人有更好的方法来实现这一点,请告诉我。

    {{#each listEntry in ListEntries}}
        {{#if ../isCheckable}}
            {{#with ../listEntry}}
                {{#view listEntryBinding="this"}}
                    {{view Ember.Checkbox viewName="checkboxView" checkedBinding="listEntry.isChecked"}}
                    <label {{bindAttr for="checkboxView.elementId"}}></label>
                {{/view}}
            {{/with}}
        {{/if}}
    {{/each}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 2015-03-28
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多