If statement 如何在polymer1.0中使用“写入条件”;dom if";?

If statement 如何在polymer1.0中使用“写入条件”;dom if";?,if-statement,polymer,If Statement,Polymer,我有以下代码: <template is="dom-if" if="{{item.hasAttach}}"> <i class="fa fa-paperclip"></i> </template> item.hasAttach=true/false 但我想检查一下这个条件,如果喜欢: item.content\u格式\u代码==“PDF” <template is="dom-if" if="{{item.content_for

我有以下代码:

<template is="dom-if" if="{{item.hasAttach}}">
     <i class="fa fa-paperclip"></i>
</template>

item.hasAttach=true/false

但我想检查一下这个条件,如果喜欢: item.content\u格式\u代码==“PDF”

<template is="dom-if" if="{{item.content_format_code == 'PDF'}}">
         <i class="fa fa-pdf"></i>
    </template>
<template is="dom-if" if="{{item.content_format_code == 'JPEG'}}">
         <i class="fa fa-jpg"></i>
    </template>
<template is="dom-if" if="{{item.content_format_code == 'xls'}}">
         <i class="fa fa-xls"></i>
    </template>

它应该像{{item.content\u format\u code=='PDF'}}=true/false 但它并不是在测试这一点。 我想按文件类型显示图标。item.content\u format\u code==“PDF”未选中此项为真/假。在polymer中,它只接受true/false作为条件实际值,但不检查表达式。
请帮帮我。

目前,polymer只支持简单的条件构造。这意味着你不能写这样的东西

[[ item.something == 'CONDITION' ]]
您只有两个选择:

  • 用于条件的项是布尔值,而不是简单的写入

    [[ item ]]
    

    会有用的。您唯一可以使用的运算符是“!”

  • 在更复杂的条件下:

  • 你可以用

    定义一个计算表达式的函数,并将其绑定到
    dom(如果

    <template is="dom-if" if="[[isFormat(item.content_format_code, 'PDF')]]">
         <i class="fa fa-pdf"></i>
    </template>
    
    Polymer({
        is: "my-element",
        isFormat: function(code, format) {
            return code === format;
        }
    });
    
    
    聚合物({
    是:“我的元素”,
    isFormat:函数(代码、格式){
    返回代码===格式;
    }
    });
    
    我刚刚做了类似的事情,如果你有权访问你的数据,只需要一个布尔值列表,比如“is PDF,is JPG,is PNG”就容易多了。然后你就可以做

    
    

    这就是我最后所做的。

    嗨,玛丽亚,谢谢你的回复,这对我真的很有帮助。写什么如果我有20个条件,它将增加代码。我不希望代码太长。我想优化代码。可能吗?嗨,拉维。我已经更新了我的答案。您可以将字符串文本(例如
    'PDF'
    )作为参数传递给函数。
    [[ _computeResult(item) ]]
    
    <template is="dom-if" if="[[isFormat(item.content_format_code, 'PDF')]]">
         <i class="fa fa-pdf"></i>
    </template>
    
    Polymer({
        is: "my-element",
        isFormat: function(code, format) {
            return code === format;
        }
    });