简体   繁体   English

聚合物双向数据绑定不起作用

[英]Polymer two-way data-binding not working

I've an issue where I update a value in the child element which doesn't propagate to the parent 我有一个问题,我更新子元素中不传播给父元素的值

So, I've two polymer elements; 所以,我有两种聚合物元素; my-parent and my-child 我的父母和我的孩子

<polymer-element name="my-parent">
    <template>
        <p>PARENT, Foo is: {{foo}}</p>
        <my-child bar="{{foo}}"></my-child>
   </template>
   <script>
       Polymer('my-parent', {});
   </script>
</polymer-element>

<polymer-element name="my-child" attributes="bar">
    <template>
        <input value="{{bar}}">
        <p>CHILD, bar is {{bar}}</p>
    </template>
    <script>
        Polymer('my-child', {
            bar: ''
        });
    </script>
</polymer-element>

DEMO DEMO

What I expected is that the value typed in the input field should be displayed within the parent template behind 我所期望的是输入字段中输入的值应该显示在后面的父模板中

<p>PARENT, Foo is: {{foo}}</p>

Any suggestions what I might be doing wrong here ? 有什么建议我可能在这里做错了吗?

The warning message in the browser console gives us a hint: 浏览器控制台中的警告消息为我们提供了一个提示:

Attributes on my-child were data bound prior to Polymer upgrading the element. 我的子项上的属性是在Polymer升级元素之前绑定的数据。 This may result in incorrect binding types. 这可能导致不正确的绑定类型。

The solution: just change the order of the two elements. 解决方案:只需更改两个元素的顺序即可。 You need to declare the child element before the parent element (in most cases this problem doesn't occur, because you import the child element before its usage). 您需要在父元素之前声明子元素(在大多数情况下,不会发生此问题,因为您在使用之前导入子元素)。

It actually works just fine. 它实际上工作得很好。 Go to http://ele.io and try it 转到http://ele.io并尝试一下

UPDATE: I actually see now the eror. 更新:我实际上现在看到的是恐怖。 Previous solution is the correct one. 以前的解决方案是正确的。 The definition order 定义顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM