1

使用 Polymer Dart,我经常需要在其中一个子元素后面获取 Polymer-Element 对象。

ButtonElement nextButton;

void inserted()
{
  //Get hold of the elements
  nextButton = shadowRoot.query('#nextButton');
  //Do some thing useful with nextButton
}

<template if="{{emailValid}}">
   <button id="nextButton" on-click="nextStep">
</template>

这工作正常。但是,如果在这种情况下 nextButton 位于条件模板下方,则在调用 insert() 时它不是 DOM 的一部分,因此找不到。还有其他方法可以掌握它吗?

否则,我将不得不确定何时显示该条件模板并随后抓取它。

4

1 回答 1

1

这可能取决于“使用 nextButton 做一些有用的事情”的确切含义,但完成此操作的 Polymer-ic 方法通常是将任何可重用行为与其操作的 DOM 一起封装。也就是说,不是#nextButton封闭元素的inserted方法中包含要操作的代码,而是创建一个新的自定义元素,让我们调用它super-button,并将相关代码放在super-button'sreadyinserted方法中。

然后,如果您发现某些确实应该在 之外的行为super-button,请遵循与您在上面使用的处理程序相同的模式on-click。在适当的时间触发super-button定义事件,然后以声明方式将处理程序映射到该事件:

<template if="{{emailValid}}">
   <super-button on-click="nextStep" on-my-special-event="mySpecialEventHandler"></super-button>
</template>
于 2013-09-19T04:54:21.507 回答