2015-04-01 177 views
1

我想知道是否有可能使用聚合物表達式來綁定一個屬性有條件的一個元素,同時也分配給它一個值,如果綁定爲true?聚合物綁定條件屬性和評估屬性

我知道如何做到既seperately:

pattern?="{{someBoolean}}" 

pattern="{{someBoolean ? 'value if true' : 'value if false'}}" 

,但在完成這兩個不使用的元素的原型定義額外的JavaScript已經失敗。

任何幫助或建議表示讚賞!

編輯:

在上面的例子中

if (someBoolean) 
<input pattern="value if true"/> 
else if (!someBoolean) 
//pattern attribute is not bound and attribute assignment does not occur 
<input/> 

回答

2

你可以做

<template if="{{ boolean }}"> 
    {{ patternIfTrue }} 
</template> 

但我認爲這可能在聚合物0.8有所改變。

+0

呃,我希望它可以。如果他們結合了這兩種行爲,那將會很好。 – andrsnn 2015-04-02 02:05:09

1

我不明白這一點。 在這種情況下,您是否需要將屬性conditionnaly與另一個值true綁定? 在所有HTML屬性中,無論您需要指定一個值,還是應從dom中刪除的conditionnaly屬性(ex:hidden =「true」),因爲某些瀏覽器不會讀取該值(例如:hidden = 「假」可能會隱藏在某些瀏覽器中)

+0

在我的情況下,我使用模式屬性和相應的正則表達式在輸入標記上使用它。將模式留空或空白會導致輸入元素顯示無效/錯誤狀態(validity.valid)的結果不一致。 – andrsnn 2015-04-02 16:21:36

+0

即使計算結果爲false,模式屬性仍然綁定到元素。 – andrsnn 2015-04-02 18:27:56

+0

哦,好的。我猜在這種情況下,你應該只使用2個變量: - patternBoolean(true/false) - htmlPattern(來自patternBoolean的計算值:請參閱polymer doc中的計算部分:https://www.polymer-project。 org/0.5/docs/polymer/polymer.html#computed-properties),或者使用patternBooleanChanged函數來計算htmlPattern: patternBooleanChanged:function(){this.htmlPattern = this.patternBoolean? 'value if true':'value if false';} 並使用pattern =「{{htmlPattern}}」 – BLASTOR 2015-04-03 21:58:05