聚合物双向数据绑定
Polymer two-way data binding
我有以下自定义元素。为了简洁起见,我只包含了相关的代码:
<dom-module id="my_custom_element">
<template>
...
</template>
</dom-module>
<script>
Polymer({
is: 'my_custom_element',
properties: {
selecteditem: {
type: String,
value: '',
notify: false
}
},
//other functions
});
请注意,所选项目的通知字段设置为 false 属性。
自定义元素的用法如下:
<my_custom_element id="myElement" selecteditem={{myselecteditem}}></my_custom_element>
如果某些原因导致自定义元素中的 selectedItem 属性 发生更改,那么我希望以下警报语句不会显示新值,因为通知设置为 false。然而,实际行为是显示新值。这是为什么?
alert($('#myElement').prop('selecteditem'));
在以下文章中:
https://www.polymer-project.org/1.0/docs/devguide/data-binding.html
它明确指出:
"If the property being bound does not have the notify flag set, only one-way (downward) binding will occur".
但我在这种情况下遇到 "upward" 绑定。
我可能是错的,但我不认为你看到的是 upward
绑定。
无论双向绑定是否启用,您 my-custom-element
上的 属性 都会发生变化,但它不会向上传播到父级 my-parent-element
。
因此 my-parent-element
中的 myProp
不会在 selectedItem
更改时更新。
<dom-module id="my-parent-element">
<template>
<my-custom-elment selecteditem={{myProp}}>
</template>
</dom-module>
我有以下自定义元素。为了简洁起见,我只包含了相关的代码:
<dom-module id="my_custom_element">
<template>
...
</template>
</dom-module>
<script>
Polymer({
is: 'my_custom_element',
properties: {
selecteditem: {
type: String,
value: '',
notify: false
}
},
//other functions
});
请注意,所选项目的通知字段设置为 false 属性。
自定义元素的用法如下:
<my_custom_element id="myElement" selecteditem={{myselecteditem}}></my_custom_element>
如果某些原因导致自定义元素中的 selectedItem 属性 发生更改,那么我希望以下警报语句不会显示新值,因为通知设置为 false。然而,实际行为是显示新值。这是为什么?
alert($('#myElement').prop('selecteditem'));
在以下文章中:
https://www.polymer-project.org/1.0/docs/devguide/data-binding.html
它明确指出:
"If the property being bound does not have the notify flag set, only one-way (downward) binding will occur".
但我在这种情况下遇到 "upward" 绑定。
我可能是错的,但我不认为你看到的是 upward
绑定。
无论双向绑定是否启用,您 my-custom-element
上的 属性 都会发生变化,但它不会向上传播到父级 my-parent-element
。
因此 my-parent-element
中的 myProp
不会在 selectedItem
更改时更新。
<dom-module id="my-parent-element">
<template>
<my-custom-elment selecteditem={{myProp}}>
</template>
</dom-module>