繁体   English   中英

将模板呈现为可信的HTML字符串[AngularJS]

[英]Rendering a template into a trusted HTML string [AngularJS]

我在这里使用AngularJS Toaster来显示我的API中的ModelState错误。 您可以通过AngularJS Toaster受信任的HTML,我想从呈现的模板中传递HTML:

<script type="text/ng-template" id="modelStateErrors.html">
    <div>{{response}}</div>
</script>

这是弹出吐司的代码:

app.service("errorService", ["$templateCache", "toaster", function ($templateCache, toaster) {
    this.catchErrors = function (response) {
        // Pass "response" to the template to render.
        toaster.pop("error", "", $templateCache.get("modelStateErrors.html"), 0, true);
    };
}]);

此代码仅生成<div>{{response}}</div> ,未解析。

编辑:所以我尝试添加:

$compile($templateCache.get("modelStateErrors.html"))(response);

我收到此错误消息

Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element

$tempateCache检索模板后,您仍需要在将其写入DOM之前对其进行编译:

AngularJS:$ compile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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