如何初始化在条目页面中声明的自定义元素的实例?
例如:
<!-- 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='.
我的代码有什么问题?
答案 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;
});
});
}