【问题标题】:Is it possible to write conditional check for two variable in one conditional check是否可以在一次条件检查中为两个变量编写条件检查
【发布时间】:2018-05-11 19:27:47
【问题描述】:

我有两个条件需要检查禁用按钮的情况。

示例代码,我的做法

<div class="{{if isallowed 'notallowed'}} {{if isloading 'notallowed'}}">Submit</div>

谢谢。

【问题讨论】:

    标签: ember.js handlebars.js htmlbars


    【解决方案1】:

    你可以这样做:

    <div class={{unless isallowed 'notallowed' (if isloading 'notallowed')}}>Submit</div>
    

    【讨论】:

      【解决方案2】:

      我喜欢在一般情况下使用ember-truth-helpers

      {{#if (and foo bar)}} foobar! {{/if}}

      对于调整类(仅限组件),我使用classNameBindings

      classNameBindings: [isUrgent]

      如果在组件上下文中 isUrgent 为真,这将向组件添加类 is-urgent。

      【讨论】:

      • 我不能使用 classNameBindings 因为提到的 div 不是组件的父 div,它是列表组件的图标。
      • 你可以使用 ember-truth-helpers 吗?
      【解决方案3】:

      我们可以通过使用 helper 来实现。

      我为此创建了助手,并且对我来说工作正常。

      助手'isany-true'

      import Ember from 'ember';
      
      export function anytrue(params) {
          return params.includes(true)
      }
      
      export default Ember.Helper.helper(anytrue);
      

      示例

      <div class="{{if (isany-true isdisableprev isloading) 'notallowed'}}">Submit</div>
      

      【讨论】:

      • Alexma 的答案是正确的选择。您可以像这样使用 ember-truth-helper 插件 class={{if (or isdisableprev isloading) 'notallowed'}}
      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 2022-10-28
      • 1970-01-01
      • 2014-09-01
      • 2014-05-08
      • 1970-01-01
      相关资源
      最近更新 更多