簡體   English   中英

如何在Polymer dart中查詢shadowRoot內容元素

[英]How to Query shadowRoot content elements in Polymer dart

如何查詢shadowRoot內容元素。 我創建了一個自定義表單元素,此表單在shadowRoot,FormInput和自定義元素之外還提供了子級。 我需要從shadowRoot中獲取所有孩子。 如果我使用shadowRoot.query或shadowRoot.queryAll,它在組件外部具有相同的效果,則我無法查看子級,因為shadowRoot對其進行了混淆。 我正在嘗試使用shadowRoot.query(“ content”)。getDistributedNodes(); 但是節點列表中的這個文本元素到底是什么,我的孩子們只有一個。

@CustomTag("v-form")
class Form extends VaderComponent {
  bool isValid(){
    bool valid = true;
    var nodes  = shadowRoot.query("content").getDistributedNodes();
    nodes.forEach((element) {
      window.console.debug(element);
      element.queryAll("v-input").forEach((elementTarget){
        if(elementTarget is FormItem){
          window.console.info("É um item de formulário");
          if(elementTarget.xtag.isValid() == false){
            valid = false;
          }
        }

      });

    });
    return valid;
  }
}

<polymer-element name="v-form">
    <template>
        <form>
            <content></content>
        </form>
    </template>
    <script type="application/dart" src="Form.dart"></script>
</polymer-element>


 <v-form id="form">
            <fieldset>
                <legend>Fieldset</legend>
                <div class="row">
                    <div class="large12 columns">

                        <v-input title="Password Example" type="password" placeholder="large-12.columns" />
                    </div>
                </div>
                <div class="row">
                    <div class="large-12 columns">
                        <v-input title="Mask Input" mask="999" type="tel" value="Valor" placeholder="large-12.columns"></v-input>
                    </div>
                </div>

                <div class="row">
                    <div clas="large-4 columns">

                        <v-input title="Input Label" type="date" placeholder="large-4.columns"></v-input>
                    </div>
                    <div class="large-4 columns">

                        <v-input title="Input Label" type="text" allowEpty="false" placeholder="Not Allow Empty Value"></v-input>
                    </div>
                    <div class="large-4 columns">
                        <div class="row collapse">

                            <div class="small-9 columns">
                                <v-input title="Input Label" type="text" placeholder="small-9.columns"></v-input>
                            </div>
                            <div class="small-3 columns">
                                <span class="postfix">.com</span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="large-12 columns">
                        <label>Textarea Label</label>
                        <textarea placeholder="small-12.columns"></textarea>
                    </div>
                </div>

            </fieldset>
        </v-form>

呼叫isValid時發生錯誤:

#text
 undefined:1
Uncaught Error: Class 'Text' has no instance method 'queryAll'.

NoSuchMethodError : method not found: 'queryAll'
Receiver: Instance of 'Text'
Arguments: ["v-input"] 

如果僅在console.debug中輸出項目,則會得到以下結果:

#text
<fieldset>​…​</fieldset>​
#text

有沒有更好的方法? 這段代碼很難看(兩個forEach(n ^ 2))

shadowRoot和的不同。 可以正常查詢,因為它是光線的一部分。 主體代碼變為:

var nodes  = this.queryAll("v-input");
nodes.forEach((element){
if(elementTarget is FormItem){
      window.console.info("É um item de formulário");
      if(elementTarget.xtag.isValid() == false){
        valid = false;
      }
}
});
return valid;

該代碼按預期工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM