How to add attributes to a dynamically created component

大城市里の小女人 提交于 2020-01-23 17:50:25

问题


I would like to add a published attribute to a dynamically created component. The code for the component is shown below:

  <!DOCTYPE html>

  <link rel="import" href="name-form.html">
  <link rel="import" href="../../shared/red-asterisk.html">

  <polymer-element name='name-view'>
    <template>

      <div id='name-view' class='flex-row-container view'>
        <section id='row0' class='flex-row' >
          <button id='add-name-btn'
                  class='button add-button'
                  on-click='{{addName}}'
                  autofocus>Add Name</button>
          <red-asterisk></red-asterisk>
        </section >

        <section id='names' class='flex-column'>

        </section>
      </div>

    </template>

     <script type="application/dart">

      import 'package:polymer/polymer.dart';
      import 'dart:html' show Event, Node, Element;

      @CustomTag( 'name-view' )
      class NameViewForm extends PolymerEment
      {

        @published String receiver = '';

        NameViewForm.created() : super.created();


        void addName( Event e, var detail, Node target )
        {
          if( $[ 'names' ].children.length < 1 )
          {
            $[ 'names' ].children
                        .add( new Element.tag( 'name-form' ) );


          }

          $['names'].on["deleteDispatch"]
                   .listen( (Event e)
                       {
                          (e.target as Element).remove();

                       });

        }
      }

    </script>
  </polymer-element>

It is the element ('name-form' created by

     $[ 'names' ].children
                 .add( new Element.tag( 'name-form' ) );

to which I would like to add an attribute receiver='patient'.

Thanks


回答1:


Is this a follow up question to Custom Events in Nested Polymer Dart UI?. In this case it would not be necessary to create a published attribute. You can just add a field like role in the class and set this field to 'nok-form' or 'patient-form'.

Element nameForm = new Element.tag( 'name-form' )
nameForm.role = 'nok-form';
 $[ 'names' ].children
                 .add(nameForm);

You only need a published attribute when you want to set the value in markup (HTML).



来源:https://stackoverflow.com/questions/20928266/how-to-add-attributes-to-a-dynamically-created-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!