【问题标题】:Bind an Ember checkbox to the opposite value of a property将 Ember 复选框绑定到属性的相反值
【发布时间】:2018-11-16 18:59:43
【问题描述】:

我有一个模型foo,属性为bar。当我渲染一个 ember 复选框时,我想将 checked 属性绑定到 foo.bar 以便在选中该框时 foo.barfalse 并且在未选中时,foo.bartrue

在做:

{{input
  type='checkbox'
  checked=foo.bar}}

得到我想要的相反,而

{{input
  type='checkbox'
  checked=(not foo.bar)}}

不将foo.bar 的值绑定到checked 属性。在不向foo 添加新的notBar 属性的情况下如何解决这个问题?

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    你可以这样做:

    <input
     type='checkbox'
     onchange={{action (mut foo.bar) (not foo.bar)}}
     checked={{not foo.bar}}>
    

    您不需要为此使用外部切换助手。

    【讨论】:

      【解决方案2】:

      这就是诀窍:

      <input
       type='checkbox'
       onchange={{action (toggle 'bar' foo)}}
       checked={{not foo.bar}}>
      

      使用https://github.com/DockYard/ember-composable-helpers#toggle

      【讨论】:

        猜你喜欢
        • 2013-03-25
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        • 2012-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多