【问题标题】:Accessing htmlElement in ShadowDom in Dart Polymer在 Dart Polymer 的 ShadowDom 中访问 htmlElement
【发布时间】:2014-01-25 10:40:04
【问题描述】:

我对 Dart 和 Polymer 很陌生,有一个问题我找不到答案。

我正在编写一个简单的单页应用程序,并希望访问聚合物元素的 shadowDom 中的 buttonElement。 (SDK 1.0 和 Polymer 0.9.3,Dart 稳定版)

就像我从文档和 api 中理解的那样,我应该使用:

@override
void enteredView() {
  super.enteredView();
  submitButton = $['submitButton'];
  submitButton.disabled = true;
}

现在这是我的 html :

<polymer-element name="..." attributes="..." >
  <template>
    ...
    <template repeat="{{ ... in ... }}">
      ...
    </template>
    <template if="{{...}}">
      ...
      <button id="submitButton" on-click="{{...}}">...</button>
    </template>
  </template>
</polymer-element>

在这个模板中,当我在 enterView 方法的末尾打印出 $['submitButton'] 时,我得到了 null。

但是:当我更改我的 html 模板并且不将按钮放在嵌套模板标签中时:

<polymer-element name="..." attributes="..." >
  <template>
    ...
    <template repeat="{{ ... in ... }}">
      ...
    </template>
    <template if="{{...}}">
      ...
    </template>
    <button id="submitButton" on-click="{{...}}">...</button>
  </template>
</polymer-element>

我得到了我的按钮并且可以使用它。否则,enteredView (button.disabled) 中的最后一次调用会引发错误。

谁能帮我解决这个问题?我做错了什么?

提前致谢。

编辑:

可以使用 Dart 中的 disabled 属性的特殊表达式来启用/禁用按钮。

<button disabled?="{{buttonDisabled}}">

这将导致删除和添加禁用属性。 在 if 或 repeat 模板中查找元素仍然不能这样工作。

【问题讨论】:

    标签: dart dart-polymer


    【解决方案1】:

    使用$['id'] 方法访问&lt;template repeate="..."&gt;template if="..."&gt; 中的元素还不起作用。
    最近在 Dart 组里有一个关于这个的讨论。我认为也应该有一个功能请求,但还没有找到它们。

    我不确定 shadowRoot.querySelector('#id') 是否有效。

    如果不是,那么 AFAIK 唯一的选择是 MutationObserver 就像这里的 dart-polymer-dart-examples / web / mutation_observers / 因为在元素插入 DOM 之前您无法访问它。

    编辑:

    【讨论】:

    • 感谢您的回复!我会试试这个编辑:submitButton = querySelector('#submitButton'); 也不起作用。
    • 您尝试时是否将&lt;template if='expr'&gt; 评估为true?如果您只想启用/禁用按钮,您可以将disabled 属性绑定到元素类&lt;button id="submitButton" disabled?={{btnSubmitDisabled}}&gt; 和代码@observable bool btnSubmitDisabled = true; 的值。那么您甚至不需要获取对按钮本身的引用。
    • 是的,我只想禁用/启用按钮并首先尝试您的建议,因为我认为它是最干净的。但是&lt;button disabled="false"&gt; 仍然被禁用,因为禁用按钮的 html 规范需要具有“禁用”属性,任何处于“禁用”状态的东西都会导致禁用按钮。是的,表达式评估为真,否则我看不到按钮。今晚我将尝试 MutationObserver,感谢所有链接!
    • Polymer 提供对此类布尔属性的支持,但您必须添加“?”在“=”之前。
    • 好的,谢谢这个作品。我认为我在网上找到的 WebUI 解决方案必须有一个等效的解决方案。我会把答案贴在顶帖。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多