dart中纸元素的实例 [英] Instance of Paper elements in dart
问题描述
如何获取下面纸质输入
元素的实例?
How can I get an instance to the paper-input
element below?
HTML文件:
<!DOCTYPE html>
<html>
<head>
<!-- <script src='packages/web_components/platform.js'></script>
not necessary anymore with Polymer >= 0.14.0 -->
<script src='packages/web_components/dart_support.js'></script>
<link rel='import' href='packages/paper_elements/paper_input.html'>
<script src='packages/browser/dart.js'></script>
</head>
<body fullbleed unresolved>
<paper-input id='subject' label='subject'></paper-input>
<script type='application/dart' src='myscript.dart'></script>
</body>
</html>
myscript.dart:
myscript.dart:
import 'dart:html';
import 'package:polymer/polymer.dart';
import 'package:paper_elements/paper_input.dart';
export 'package:polymer/init.dart';
PaperInput _subject = querySelector('#subject'); // exception
void main() {
...
}
这会导致异常:
Breaking on exception: type 'HtmlElement' is not a subtype of type 'PaperInput'
从HtmlElement投射到PaperInput不起作用。建议包括使用 shadowRoot.querySelector('#subject');
和其他 shadowRoot
使用,从?如果我将 .shadowRoot.querySelector('paper-input')
附加到myscript.dart的最后一行,那么元素为null。如果我使用id而不是标记名称,结果相同。元素本身是html主体的顶层,因此它不在任何其他shadowRoot。
Casting from HtmlElement to PaperInput does not work. Suggestions have included using shadowRoot.querySelector('#subject');
and other shadowRoot
usages, but where does it come from? If I append .shadowRoot.querySelector('paper-input')
to the last line of myscript.dart, the element is null. Same result if I use the id instead of tag name. The element itself is top-level to the html body, so it is not in any other shadowRoot.
没有办法获取<$ c $的内容c> paper-input ,而不是PaperInput类。
There is no way to get the contents of the paper-input
without it being of the class PaperInput. But there seems no way to cast it.
推荐答案
当你像下面显示的那样或者在Polymer元素(<$ c $在 super.attached()
后或在某些点击或其他事件处理程序(当元素正确初始化时)
When you do it like shown below or inside a Polymer element (attached()
after super.attached()
or in some click or other event handler (when the element is properly initialized)
main() {
initPolymer().run(() {
// code here works most of the time
Polymer.onReady.then((_) {
// some things must wait until onReady callback is called
// for an example look at the discussion linked below
PaperInput _subject = querySelector('#subject');
});
});
}
这篇关于dart中纸元素的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!