我的Dart Web应用程序中有2个“视图”(屏幕/页面),我想异步获取View#2的HTML,然后将其绑定到Dart DOM元素用户仍在查看View#1 。这样,当用户点击View#1上的按钮时,它会将它们重定向到View#2,所有HTML,代码等都已准备就绪,应用程序可以立即显示它们的View#2(所以它们不必等待页面加载等。)。
要获取View#2的HTML,我想我需要类似的东西:
class View2 extends AbstractView {
ButtonElement redButton;
ButtonElement blueButton;
// constructors, getters/setters, click handlers for the buttons, etc.
// Ex: here url might be: "http://example.com/myapp/view-2.html".
void fetchAndBindUI(String url) {
HttpRequest.getString("view-2.html").then((html) {
// HERE'S THE PROBLEM:
// How to associate querySelector with "html" instead of what's
// currently inside the window.document (browser DOM)?
redButton = querySelector("#redButton") as ButtonElement;
blueButton = querySelector("#blueButton") as ButtonElement;
});
}
}
在上面的代码中,当我执行querySelector("#redButton") as ButtonElement
时,由于View#1当前被“加载”到window.document中,它将尝试在View#1中找到一个名为redButton
的元素。相反,我需要它来搜索“redButton" inside the HTML returned by the HTTP GET to
view-2.html`。我该怎么做?
答案 0 :(得分:2)
您可以在html
中注入div
并在其中搜索:
DivElement div = new DivElement();
div.innerHtml = html;
ButtonElement redButton = div.querySelector("#redButton");