简体   繁体   English

Polymer的双向数据绑定不适用于元素的子级属性值

[英]Polymer's two way data-binding not working in an element's child's attribute value

I have a product-card custom element that looks like this: 我有一个product-card自定义元素,如下所示:

<dom-module id="product-card">    
   <template>
    <div class="card-main-content layout horizontal center">
      <content select="img"></content>
      <content select="h3"></content>
      <content select="h4"></content>
    </div>
    <content></content>
    <paper-icon-button id="carticon" icon="add-shopping-cart" on-tap="cartTapped">
    </paper-icon-button>
  </template>
</dom-module>

<script>
(function() {
  Polymer({
  is: 'product-card',

  properties: {
    host: {
      type: String,
      notify:true,
      reflectToAttribute: true,
      value: "http://localhost:3003"
    },
    imagepath: {
      type: String,
      notify: true,
      reflectToAttribute: true
    },
    imageurl: {
      type: String,
      notify: true,
      reflectToAttribute: true,
      computed: 'computeImageUrl(host, imagepath)'
   }
  },

  cartTapped: function(event) {
    /*...............*/
    this.fire('cart-tap');
  },

    computeImageUrl: function(host, imagepath) {
     return host+imagepath;
    }
  });
  })();
</script>

This element is used in another custom element called product-list like this: 此元素用在另一个称为product-list自定义元素product-list如下所示:

<dom-module id="product-list">
  <template>
    <get-products-service products="{{products}}" id="productservice"></get-products-service>
    <div>
      <template is="dom-repeat" items="{{products}}">
        <product-card on-cart-tap="handleCart" imagepath="{{item.master.images.0.small_url}}">
          <img src="{{imageurl}}" width="100" height="100">
          <h3>{{item.name}}</h3>
          <h4>{{item.display_price}}</h4>
       </product-card>
     </template>
    </div>
  </template>
</dom-module>

The problem i have is that the src={{imageurl}} resolves to null. 我的问题是src={{imageurl}}解析为null。 The imageurl is being calculated as i can see the expected values being reflected on the attributes when i inspect with chrome's developer tools. 正在计算imageurl ,因为当我使用chrome的开发人员工具进行检查时,可以看到期望值反映在属性上。 Just to add, when i add an imageurl attribute like this: 只是要添加,当我添加像这样的imageurl属性时:

<product-card on-cart-tap="handleCart" imagepath="{{item.master.images.0.small_url}}" imageurl="{{imageurl}}">
  <img src="{{imageurl}}" width="100" height="100">
  <h3>{{item.name}}</h3>
  <h4>{{item.display_price}}</h4>
</product-card>

I get a url of the first product's image which does not change as the product's array is iterated on. 我得到第一个产品的图像的URL,该URL在迭代产品的数组时不会改变。 This results to loading of the same image for all products which is not the desired result. 这将导致为所有产品加载相同的图像,这不是理想的结果。

Good question. 好问题。 Right now I am also struggling to understand how to use binding within the light dom. 现在,我也在努力地了解如何在光影dom内使用绑定。 If I find a solution, I will be sure to post it here. 如果找到解决方案,请确保将其发布在此处。

You could move the image insertion out of the product-list component and into the product-card element. 您可以将图像插入从产品列表组件中移出,并移到产品卡元素中。 It is not what you are looking for in terms of flexibility, but you get the desired effect. 您并不是在灵活性方面寻求什么,但是您会获得理想的效果。

<dom-module id="product-card">    
<template>
<div class="card-main-content layout horizontal center">
  <img src="{{imageurl}}" width="100" height="100">
  <content select="h3"></content>
  <content select="h4"></content>
</div>
<content></content>
<paper-icon-button id="carticon" icon="add-shopping-cart" on-tap="cartTapped">
</paper-icon-button>
</template>
</dom-module>

Another 'hacky' way of getting your desired result is to set the image src in the ready callback of the lifecycle. 获得理想结果的另一种“ hacky”方法是在生命周期的就绪回调中设置映像src。 Access the image element inside lightdom by using Polymer.dom(this).querySelector('img') and set the src of your computed value. 通过使用Polymer.dom(this).querySelector('img')访问lightdom内部的图像元素,并设置计算值的src。 Accessing the lightdom using various selectors is described in Local DOM Basics and API Add the ready function to your product-card element: Local DOM Basics和API中介绍了使用各种选择器访问lightdom的步骤。将ready函数添加到product-card元素中:

Polymer({
  is: 'product-card',
  ready: function() {
      Polymer.dom(this).querySelector('img').src = this.imageurl;
  },
  properties: {
    host: {
      type: String,
      notify:true,
      reflectToAttribute: true,
      value: "http://lorempixel.com/50/50"
    },
...

Try src$="{{imageurl}}" , as this will bind to the src attribute rather than the src property. 尝试使用src$="{{imageurl}}" ,因为这将绑定到src属性而不是src属性。

https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#attribute-binding https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#attribute-binding

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

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