簡體   English   中英

如何用“dom-if”在polymer1.0中寫入條件?

[英]How do I write condition in polymer1.0 with “dom-if”?

我有下面的代碼:

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

item.hasAttach = true / false

但是我想檢查條件,如果: item.content_format_code =='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_format_code =='PDF'}} = true / false但它不是測試它。 我想根據文件類型顯示圖標。 item.content_format_code =='PDF'未選中true / false 在聚合物中,它僅將真/假作為條件實際值,但不檢查表達式。 請幫我。

您可以使用計算綁定

定義一個計算表達式並將其綁定到dom-if的函數。

<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;
    }
});

目前,聚合物僅支持簡單的條件構造。 這意味着你不能寫出類似的東西

[[ item.something == 'CONDITION' ]]

你有兩個選擇:

  1. 用於條件的項是布爾值,而不是簡單的寫

     [[ item ]] 

    要么

     [[ !item ]] 

    將工作。 您可以使用的唯一操作符是'!'

  2. 對於更復雜的條件, 使用計算綁定

     [[ _computeResult(item) ]] 

我剛剛做了類似的事情,如果你有權訪問你的數據,那么只需要一個像“is-PDF,is-JPG,is-PNG”這樣的布爾列表就容易多了。 那你就可以做;

<template is="dom-if" if="[[item.is-PDF]]">

這就是我最終做的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM