Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to insert HTML coming from a JS function, which is executed when the body is loaded. Initially the body is empty.

This HTML has AngularJS commands, but the problem is that AngularJS doesn't parse the 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>

The content of controller.js is:

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."
    }
    ];
}
share|improve this question
 
I believe document.write() actually wipes the whole page, not only the body, so the AngularJS scripts as well. So even if they would process HTML added later on, it wouldn't have a chance to. –  11684 Oct 23 at 15:36
add comment

1 Answer

The onload code is executed after AngularJS actually parses the DOM. So to catch them into Angular you have to use $compile. Look in the docs about how $compile works.

share|improve this answer
 
stackoverflow.com/questions/11699635/… is a good reference on SO. –  Brandon Tilley Dec 1 '12 at 5:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.