将编译的模板渲染为字符串

时间:2014-04-10 12:03:59

标签: javascript angularjs

这是我的模板:

<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>

这是我的代码:

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();

在上面的代码片段中,如何将链接元素渲染为字符串,以使ng-repeat指令呈现错误列表?

如果我这样做......

linked.html();

...即使有验证错误,我也会得到以下内容......

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

1 个答案:

答案 0 :(得分:0)

喜欢这个......

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