Polymer 1.0 Child-to-host 一种数据绑定方式 - 只读属性
Polymer 1.0 Child-to-host one way data binding - readOnly properties
在 Polymer 1.0 中,我想要一种从 child 元素到主机的单向数据绑定。如果在定义 property.Explained here.
时定义 notify: true
和 readOnly: true
就可以做到这一点
另请参阅下面的示例。
Child:
...
Polymer({
is: "child-element",
properties:{
myProp:{
type: Number,
notify: true,
readOnly: true,
value: 0
}
}
});
主持人:
...
<child-element my-prop="{{hostValue}}"></child-element>
...
因为有 {{ ... }}
并且 属性 设置为 notify: true
主机将尝试 two-way 绑定但是 child 有 readOnly: true
.所以绑定将只工作 child-to-host.
在我的设置中,我想更改 child 中的值。因此主机中的绑定值将相应更改。但是由于 属性 被标记为 readOnly: true
,因此 child 也无法更改该值。
有没有办法从 child-to-host 绑定一种方式,防止主机覆盖值但允许 child 这样做?
更新:我准备了一个 Plunk here。转到 child-element.html。您看到 myProp
的值是 'Initial value'
。当您单击按钮时没有任何反应,因为它被设置为 readOnly
。但是,如果您在第 19 行 上注释掉 readOnly
,然后单击该按钮,您将看到值发生变化。因此 readOnly 还可以防止在 child 元素中重写 with 的值。
请使用 _setMyProp(newValue) 更改子元素中的只读值。您可以在 https://www.polymer-project.org/1.0/docs/devguide/properties.html#read-only
中阅读更多相关信息
更新了 plunk http://embed.plnkr.co/ggYJQ4z65Wj4uuBe4kUg/preview
在 Polymer 1.0 中,我想要一种从 child 元素到主机的单向数据绑定。如果在定义 property.Explained here.
时定义notify: true
和 readOnly: true
就可以做到这一点
另请参阅下面的示例。
Child:
...
Polymer({
is: "child-element",
properties:{
myProp:{
type: Number,
notify: true,
readOnly: true,
value: 0
}
}
});
主持人:
...
<child-element my-prop="{{hostValue}}"></child-element>
...
因为有 {{ ... }}
并且 属性 设置为 notify: true
主机将尝试 two-way 绑定但是 child 有 readOnly: true
.所以绑定将只工作 child-to-host.
在我的设置中,我想更改 child 中的值。因此主机中的绑定值将相应更改。但是由于 属性 被标记为 readOnly: true
,因此 child 也无法更改该值。
有没有办法从 child-to-host 绑定一种方式,防止主机覆盖值但允许 child 这样做?
更新:我准备了一个 Plunk here。转到 child-element.html。您看到 myProp
的值是 'Initial value'
。当您单击按钮时没有任何反应,因为它被设置为 readOnly
。但是,如果您在第 19 行 上注释掉 readOnly
,然后单击该按钮,您将看到值发生变化。因此 readOnly 还可以防止在 child 元素中重写 with 的值。
请使用 _setMyProp(newValue) 更改子元素中的只读值。您可以在 https://www.polymer-project.org/1.0/docs/devguide/properties.html#read-only
中阅读更多相关信息更新了 plunk http://embed.plnkr.co/ggYJQ4z65Wj4uuBe4kUg/preview