简体   繁体   English

将默认属性设置为Polymer内容标签

[英]Set default attribute to a Polymer content tag

I am building an element with Polymer with this structure: 我正在使用具有以下结构的Polymer构建一个元素:

  <template>

    <style>
      ...
    </style>

    <div class="card-header" layout horizontal center>
      <content select="img"></content>
      <content select="h1"></content>
    </div>
    <content></content>

  </template>

  <script>

    Polymer({});

  </script>
</polymer-element>

I try to set a default value for the src attribute of the <content select="img"> in case its not set deliberately inside the <e-card> element: 我尝试为<content select="img">src属性设置默认值,以防未在<e-card>元素内故意设置它的情况:

...
<script>

  Polymer({
     srcimg: 'default-picture.png'
  });

</script>
...

On browser the value in the script is applied in the <content select="img"> at the Shadow DOM but not in the <img> element inside the <e-card> . 在浏览器上,脚本中的值应用于Shadow DOM的<content select="img"> ,而不应用于<e-card>内的<e-card> <img>元素中。 How can I apply the default value for the src attribute in the <img> element? 如何在<img>元素中为src属性应用默认值?

it might be easier to just go ahead and make your image tag in the element and publish the the src attribute out side your element. 继续操作并在元素中添加图片标签,然后将src属性发布到元素外可能会更容易。 then you could set the default path in js and still have the content be user configurable. 那么您可以在js中设置默认路径,但仍然可以让用户配置内容。

<polymer-element name="test-element" attributes="img height width">
<template>

<style>
  ...
</style>

<div class="card-header" layout horizontal center>
  <img src="{{img}}" height="{{height}}" width="{{width}}">
  <content select="h1"></content>
</div>
<content></content>

</template>

<script>

Polymer({
  ready: function () {
    this.img = "default image url";
    this.height = "100px";
    this.width = "100px"
  }
});

</script>
</polymer-element>

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

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