Double condition with #if

别说谁变了你拦得住时间么 提交于 2019-12-21 09:29:31

问题


How to do a double condition {{#if person && work}} ? The && seems not a valid operator


回答1:


I don't believe handlebars supports multiple items in the {{#if}} statement (related: Logical operator in a handlebars.js {{#if}} conditional).

You could collapse the multiple values in your controller/view into a single computed property and check that single value in the template. This new computed property will update when either of the original values update:

App.ValuesTestController = Ember.Controller.extend({
    value1: false,
    value2: true,
    value1and2: function(){
            return this.get('value1') && this.get('value2');
    }.property('value1', 'value2')
});

Your template would look like this:

<div>{{#if value1 }}value 1 true{{/if}}</div>
<div>{{#if value2 }}value 2 true{{/if}}</div>
<div>{{#if value1and2 }}value 1 and 2 true{{/if}}</div>



回答2:


Even if the @CraigTeegarden's answer is the elegant one, I have found some cases where this workaround has made my life easier, and therefor it has made me happier:

{{#if person}}
  {{#if work}}
    Yeah!
  {{/if}}
{{/if}}


来源:https://stackoverflow.com/questions/14149415/double-condition-with-if

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!