如何初始化在条目页面中声明的自定义元素的实例

时间:2014-07-04 16:20:30

标签: dart dart-polymer

如何初始化在条目页面中声明的自定义元素的实例?

例如:

<!-- index.html -->
<polymer-element name='my-elm' attributes="count" noscript>
<template>
<span>{{count}}</span>
</template>
</polymer-element>

<my-elm id="my-elm"></my-elm>
// index.dart
(querySelector("#my-elm") as dynamic).count = 1;

当我尝试初始化my-elm聚合物元素实例的计数属性时,我得到以下执行:Breaking on exception: Class 'HtmlElement' has no instance setter'count='.我的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

修改

您只能以这种方式访问​​支持类的字段 如果您没有课程,可以使用

访问它
querySelector("#my-elm").attributes['count'] = '1'; // attributes can only store strings

编辑结束

您需要在index.dart中延迟代码,直到Polymer准备就绪

import "package:polymer/polymer.dart";

main() {
  initPolymer().run(() {
    // code here works most of the time
    Polymer.onReady.then(() {     
      // some things must wait until onReady callback is called
      (querySelector("#my-elm") as dynamic).count = 1;
    });
  });
}