2017-01-16 98 views
0

更好的办法我有以下元素:通过绑定到子元素聚合物1.0

<link rel="import" href="../my-control/my-control.html" /> 

<dom-module id="my-page"> 
    <template> 
     <!-- pass the value down to the children element --> 
     <my-control my-property="{{myProperty}}"></my-control> 
    </template> 

    <script> 
     Polymer({ 
     is: 'my-page', 

     properties: { 
      myProperty: { 
       type: String, 
       value: '', 
       reflectToAttribute: true 
      } 
     } 
     }); 
    </script> 
</dom-module> 

这是“孩子”控制

<dom-module id="my-control"> 
    <template> 
     <!-- print the value of the property --> 
     <span>{{myProperty}}></span> 
    </template> 

    <script> 
     Polymer({ 
     is: 'my-control', 

     properties: { 
      myProperty: { 
       type: String, 
       value: '', 
       reflectToAttribute: true 
      } 
     } 
     }); 
    </script> 
</dom-module> 

这是我如何使用它。我这样做是因为实际上"my-page"并不真的需要一个属性,但是"my-control"可以,它包含在"my-page"的DOM中。有没有其他办法可以做到这一点?因为我传递了一个模型,而且我必须按属性为每个属性反序列化它?

<link rel="import" href="/my-page/my-page.html" /> 

<!-- declare parent element and value --> 
<my-page my-property="ABCDE-12345"></my-page> 
+1

不是。我能想到的唯一方法是在js select'my-page'访问它的'shadow dom'(方法在V0和V1规范中有所不同),然后选择'my-control'并赋值。 – a1626

+0

我希望像Angular这样的“父”数据绑定语法有,但我加倍检查,我没有找到任何有关聚合物1.x文档 – Raffaeu

+1

聚合物 – a1626

回答

-1

你的问题表达了为了通过给孩子传递属性重新声明父一子属性的关注。实际上,如果父项完全未使用父项(除非父项的用户需要在该属性上进行双向数据绑定),则无需在父项中重新声明属性。

以下是声明性示例,在父项中省略了属性重新声明

<dom-module id="x-foo"> 
    <template> 
    <my-page foo="123"></my-page> 
    </template> 
    <script> 
    Polymer({is: 'x-foo'}); 
    </script> 
</dom-module> 

<dom-module id="my-page"> 
    <template> 
    <my-element foo="[[foo]]"></my-element> 
    </template> 
    <script> 
    // no property declaration needed for one-way binding 
    Polymer({is: 'my-page'}); 
    </script> 
</dom-module> 

codepen

旁注:你不需要设置reflectToAttribute数据绑定。

+0

没有什么但这正是我在做什么... – Raffaeu

+0

@Raffaeu我承认它*看起来*非常类似于你已经在做的事情,但它不完全一样。你的问题表达了对不必要地在家长中声明财产的担忧,我的回答表明,如果你只是直接传递给孩子,那么实际上并不需要这样做。我添加了一个部分来表明父代中的重新声明只会用于双向绑定,并且可能会使问题变得模糊,因为很明显,您根本不需要父项(或父母的用户)。我会更新我的答案,至少澄清一点。 – tony19