0

我有一种情况,我的 Dart Web 应用程序中有 2 个“视图”(屏幕/页面),我想异步获取视图 #2 的 HTML,然后在用户仍在查看视图时将其绑定到 Dart DOM 元素#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 toview-2.html”。我该怎么做?

4

1 回答 1

2

您可以注入htmladiv并在其中搜索:

DivElement div = new DivElement();
div.innerHtml = html;
ButtonElement redButton = div.querySelector("#redButton");
于 2013-12-31T18:41:36.553 回答