我在 Dart 中有一些嵌套模板,如下所示:
<template id="id1">
<template id="id2">
<input type= id={{xxx}}>
</template>
</template>
这种情况下,如何查询输入元素呢?我尝试了嵌套 Timer.run 但它不起作用。
query("#id1").model = xxxx
Timer.run({ query("#id2").model=xxxxx;
Timer.run({ print(query(xxx));});
});
有人有什么建议吗?
谢谢!
================================================== =========================
注意:嵌套模板实际上是在一个表格中,代码如下:
<div style="cursor:crosshair; -webkit-user-select: none; user-select: none;">
<table id="base_table" style="border-collapse:collapse; position: absolute; top:200px; left:200px; width:800px; height:400px" border="2">
<template id="table" repeat>
<tr>
<template id={{id}} repeat>
<td> <label style="border:0px; height:18px; width:80px" id={{Cell_ID}}> {{Cell_data}} </label> </td>
</template>
</tr>
</template>
</div>
最佳答案
你最好的选择是使用 MutationObserver:
final observer = new MutationObserver(() {
... code here ...
observer.disconnect();
})..observe(node, childList: true, subtree: true);
很快,该方法将在 PolymerElement 中可用:
onMutation(() => ... code here ...);
它会自动断开观察,如上所述。实现很简单:
void onMutation(Node node, void listener(MutationObserver obs)) {
final observer = new MutationObserver(() {
listener(observer);
observer.disconnect();
})..observe(node, childList: true, subtree: true);
}
因此,对于您的特定示例,粘贴 onMutation 函数,然后像这样编写:
var id1 = query("#id1");
id1.model = xxxx
onMutation(id1.parent, (_) {
var id2 = query("#id2");
id2.model = xxxxx;
onMutation(id2.parent, (_) {
print(query(xxx));
});
});
关于html - 在 Dart 中如何查询嵌套模板中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18908009/