简体   繁体   中英

Render compiled template to string

Here's my template:

<script type="text/ng-template" id="validationErrors.html">
    <div id="validationErrors">
            <div id="errorListContainer">
                <h2>Your order contains invalid fields:</h2>
                <ul>
                    <li ng-repeat="error in validationErrors">{{error.message}}</li>
                </ul>
            </div>
    </div>
</script>

Here's my code:

var errors = [...];
var template = $templateCache.get("validationErrors.html");
var link = $compile($.trim(template));
var scope = $rootScope.$new(true);
scope.validationErrors = errors;
var linked = link(scope);
alert(/* what goes here? */);
scope.$destroy();

In the above snippet, how do I render my linked element to a string such that the ng-repeat directive renders out the list of errors?

If I do this...

linked.html();

... then I get the following even though there are validation errors...

<DIV id=errorListContainer>\r\n<H2>Your order contains invalid fields:</H2>\r\n<UL><!-- ngRepeat: error in validationErrors --></UL></DIV>

Like this...

scope.$apply()
alert(linked.html());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM