Question

I am trying to create a ParagraphElement at run time and it to Div element in a polymer component as follows:

<polymer-element name="my-element" extends="div">
  <template>
    <p>
      <button on-click="updateMessage" data-msg="You clicked Show">Show Message</button>
      <button on-click="updateMessage" data-msg="You clicked Hide">Hide Message</button>
      <button on-click="addPelement" >Add P element</button>            
    </p>
    <p id="message">{{ message }}</p> <!-- bind the message -->
    <div id="mydiv"></div>  
  </template>
  <script type="application/dart" src="my_element.dart"></script>
</polymer-element> 

and dart file is as follows:

@CustomTag('my-element')
class MyElement extends PolymerElement with ObservableMixin {
  @observable var message;

  void updateMessage(Event e, var detail, Element target) {
    message = target.attributes['data-msg']; // extract the data- attribute
  }

  void addPelement(Event e) {
    var item = new ParagraphElement (); 
    item.text="new p element";
    DivElement d = query("#mydiv");
    d.children.add(item);
  }
}

The line query("#mydiv") returns a null element.

Was it helpful?

Solution 2

needed to add new element to ShadowRoot

DivElement d =  this.shadowRoot.query("#mydiv");

OTHER TIPS

You need to use the following code to access your div element:

DivElement d = $['mydiv'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top