3

我想插入来自 JS 函数的 HTML,该函数在加载正文时执行。最初身体是空的。

这个 HTML 有 AngularJS 命令,但问题是 AngularJS 不解析 HTML。

<!doctype html>
<html ng-app>
    <head>
        <script>
            function loadHTML(){
                html = '<ul ng-controller="phoneCtrl"><li ng-repeat="phone in phones">{{phone.name}}<p>{{phone.snippet}}</p></li></ul><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script'+'><script src="controllers.js"></script'+'>';
                document.write = html;
            }
        </script>
    </head>
    <body onload="loadHTML();" ></body>
</html>

controller.js 的内容是:

function phoneCtrl($scope) {
    $scope.phones = [
    {
        "name": "Nexus S",
        "snippet": "Fast just got faster with Nexus S."
    },

    {
        "name": "Motorola XOOM™ with Wi-Fi",
        "snippet": "The Next, Next Generation tablet."
    },

    {
        "name": "MOTOROLA XOOM™",
        "snippet": "The Next, Next Generation tablet."
    }
    ];
}
4

1 回答 1

3

onload 代码在 AngularJS 实际解析 DOM 之后执行。因此,要将它们捕获到 Angular 中,您必须使用 $compile。查看有关 $compile 如何工作的文档。

于 2012-12-01T05:38:49.203 回答