【问题标题】:Two-way binded propery in custom polymer element定制聚合物元件中的双向结合特性
【发布时间】:2017-07-26 16:44:20
【问题描述】:

我想模块化我的部分代码。我创建了一个与数组和数字一起使用的自定义元素。使用双向绑定,这应该不是问题。它是。似乎孩子在准备好之前就获得了财产。

<link rel="import" href="custom-element.html">

<dom-module id="is-parent">
    <custom-element p1="{{p1}}"></custom-element>
    <script>
        Polymer({
            is: 'is-parent',
            properties: {
                p1:{
                    type: Number,
                    notify: true,
                    observer:"obsParent",
                    value:0,
                },
            },
            obsParent: function(newValue, oldValue) {
                console.log(oldValue);
                console.log(newValue);
                console.log(this.p1);
            },
            ready: function(){
                this.p1= 0;
            },
    </script>
</dom-module>


<dom-module id="is-child">

    <script>
        Polymer({
            is: 'is-child',
            properties: {
                p1:{
                    notify: true,
                    observer:"obsChild",
                },

                p2:{
                    type: Boolean,
                    computed: 'p2Computer(p1)',
                },

            },
            obsChild: function(newValue, oldValue) {
                console.log(oldValue);
                console.log(newValue);
                console.log(this.p1);
            },

            p2Computer:function(p1){
                if(p1===0){
                    return true;
                }
                return false;
            },
            ready: function(){
                console.log(this.p1);
            },
    </script>
</dom-module>

我的双向绑定属性在子级中设置为未定义,在父级中设置为 0。这个例子被简化了很多,但我的测试支持我的说法,即孩子得到一个未定义的属性,即使在父级中设置为 0,该属性仍然是未定义的。

【问题讨论】:

    标签: html data-binding properties polymer custom-element


    【解决方案1】:

    由于某种原因,您将 prop1 作为正在编辑的属性,但如果您希望它绑定到 is-child 的 p1,它应该是 p1

    <custom-element prop1="{{p1}}"></custom-element>
    

    <custom-element p1="{{p1}}"></custom-element>
    

    【讨论】:

    • 这是一个错字,在我的 sudo 代码中。已更正。
    • 好的。您是否将is-child 中的p1reflectToAttribute 设置为true?此外,在上面的 sudo 代码中,您没有在 is-child 中将类型设置为 Number for p1
    • 抱歉,问题出在命名上。 p1 的真名是 orIndex,我忘了写成 or-index="{{orIndex}}"。
    • 哈哈是的。它发生了。我建议使用聚合物棉绒来防止愚蠢的虫子。 marketplace.visualstudio.com/items?itemName=polymer.polymer-ide
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多