how to dynamically append an element to dom-if in Polymer?

前端 未结 2 1441
执笔经年
执笔经年 2021-01-19 07:09

My goal is to append an element to existing dom-if dynamically. Problem is that after appending I can see appended element in the DOM three but it never reacts

2条回答
  •  粉色の甜心
    2021-01-19 07:47

    How about adding a span in your dom-if and appending it to that span?

    Update after some comments : We need to use this.async for the item to be found. Using the ready-event only works when the condition is true initially. So you could append the element in a conditionChanged-observer - this is a working example :

    
      
    
    
    
    

    Try it here : http://plnkr.co/edit/1IIeM3gSjHIIZ5xpZKa1?p=preview .

    A side-effect of using dom-if in this case is that after setting the condition to false, the element disappears completely and gets added on the next condition-change again. So every change before setting the condition to false gets lost. You could work around it by putting the added element somewhere hidden when the condition changes and getting it back later, but I don't think this is a good idea, if the following is an alternative :

    The Polymer-team recommends using dom-if only if there is no other way, like hiding the element. So, if it is possible you also could do something like this (condition has to be true to hide the element) :

    
      
    
    
    
    

    Try it here : http://plnkr.co/edit/mCtwqmqtCPaLOUveOqWS?p=preview

提交回复
热议问题