2017-01-13 8 views
2

私は<template is="dom-if"...を使用して、条件によって異なるHTMLコンテンツを提供するPolymer要素を持っています。ポリマーdom-if:どのように負の条件を実装しますか?

ポリマーdom-ifにはその他の条件がないため、シミュレーションするには条件が負の場合が必要です。このような

何か:のみ

<link href="https://polygit.org/components/polymer/polymer.html" rel="import"> 
 

 
<dom-module id="test-thing"> 
 
    <template> 
 
    <template is="dom-if" if="{{title}}" restamp> 
 
     <b>[[title]]</b> 
 
    </template> 
 
    <template is="dom-if" if="{{!title}}" restamp> 
 
     <i>no title</i> 
 
    </template> 
 
    </template> 
 
    <script> 
 
    Polymer({ 
 
     is: 'test-thing', 
 
     properties: { 
 
     title: String 
 
     } 
 
    }); 
 
    </script> 
 
</dom-module> 
 

 
<div> 
 
    With negative condition: 
 
    <test-thing></test-thing> 
 
</div> 
 
<div> 
 
    With positive condition: 
 
    <test-thing title="Has Title"></test-thing> 
 
</div>

動作しません - 負の状態が渡されません。

これはどのように実装する必要がありますか?

+0

あなたのコードが動作する必要があります。どのポリマーのバージョンを使用していますか? –

+0

@MojtabaSafaeian 1.0はChrome 55でそのスニペットを実行しようとします - うまくいきません。 – Keith

+0

https://jsfiddle.net/ryanwtyler/6g16qknL/#&togetherjs=w0neSat7uN –

答えて

4

あなたのタイトルプロパティのデフォルト空の値を使用する必要があります。

title:{type: String,value:''} 

ので、同様:titleプロパティに値がない場合

<link href="https://polygit.org/components/polymer/polymer.html" rel="import"> 
 

 
<dom-module id="test-thing"> 
 
    <template> 
 
    <template is="dom-if" if="{{title}}" restamp> 
 
     <b>[[title]]</b> 
 
    </template> 
 
    <template is="dom-if" if="{{!title}}" restamp> 
 
     <i>no title</i> 
 
    </template> 
 
    </template> 
 
    <script> 
 
    Polymer({ 
 
     is: 'test-thing', 
 
     properties: { 
 
     title: {type: String,value:''} 
 
     } 
 
    }); 
 
    </script> 
 
</dom-module> 
 

 
<div> 
 
    With negative condition: 
 
    <test-thing></test-thing> 
 
</div> 
 
<div> 
 
    With positive condition: 
 
    <test-thing title="Has Title"></test-thing> 
 
</div>

+0

乾杯、それは動作します(+1とans)。それがなぜ必要なのか?確かにデフォルトがなければ、未定義のタイトルは 'undefined'ですが、これはまだ嘘です。 – Keith

+2

Polymerは、まだ設定されていない変数で式を描画しません。計算されたプロパティもこのように動作します。 [続きを読む](https://www.polymer-project.org/1.0/docs/devguide/observers#simple-observers) – alesc

関連する問題