![](/img/trans.png)
[英]CheckBox disabled attribute bound to Polymer Property's sub-property
[英]Checkbox's checked property binding - Polymer
我試圖將Polymer
屬性綁定到CheckBox
的checked屬性。 但是,該屬性的觀察者永遠不會被解雇,此外,該標簽也從不顯示任何文本。
但是,每次單擊CheckBox
時我都能執行一個函數。
這是我的代碼:
<link rel="import" href="../../components/polymer/polymer.html">
<dom-module id="check-box-example">
<template>
<div>
<label>
<template if="{{checked}}">Uncheck</template>
<template if="{{!checked}}">Check</template>
</label><br>
<input type="checkbox" checked="{{checked}}" on-click="_checkBoxClicked">Check Box
</div>
</template>
<script>
Polymer({
is: 'check-box-example',
properties:{
checked: {
type: Boolean,
observer: '_checkedChanged'
}
},
_checkBoxClicked: function() {
console.log("The Check Box was clicked.");
},
_checkedChanged: function(newValue, oldValue) {
console.log("New Checkbox value: " + newValue);
},
});
</script>
</dom-module>
我究竟做錯了什么? 提前致謝。
一些問題:
您的模板缺失is="dom=if"
,因此它在您的代碼中無效。
即使使用了dom-if
, if
屬性也會設置為checked
,它沒有初始值。 僅當綁定屬性具有undefined
值時才會計算綁定,並且由於從未設置checked
,因此模板不會標記任何內容(即,您不會看到“Check”或“Uncheck”)。
properties: { checked: { type: Boolean, value: false // initial value required for binding } }
您的模板文字向后看。 if="{{checked}}"
的文本內容為“取消選中”,而if="{{!checked}}"
為“Check”。 也許這些是用戶指令而不是復選框狀態。
本機input
不會為其checked
屬性發出更改事件,因此綁定不會更新已checked
屬性。 相反,您可以更新Click-handler以顯式設置checked
屬性以匹配已checked
的input
值。
_checkBoxClicked: function(e) { this.checked = this.$.input.checked; }
您的label
與input
無關,因此單擊它不會更改checkbox
的狀態。 你可以解決這個問題,通過使用label
的for
:
<label for="foo">...</label> <input id="foo">
或通過input
label
的子項:
<label> <input> </label>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.