简体   繁体   English

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

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

I'm using AngularJS Toaster found here to show ModelState errors from my API. 我在这里使用AngularJS Toaster来显示我的API中的ModelState错误。 You are able to pass AngularJS Toaster trusted HTML and I'd like to pass it HTML from a rendered template: 您可以通过AngularJS Toaster受信任的HTML,我想从呈现的模板中传递HTML:

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

Here's the code for popping up the toast: 这是弹出吐司的代码:

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);
    };
}]);

This code only produces the <div>{{response}}</div> as is, unparsed. 此代码仅生成<div>{{response}}</div> ,未解析。

EDIT: So I tried adding: 编辑:所以我尝试添加:

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

and I get this error message 我收到此错误消息

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

Once you retrieve the template from the $tempateCache , you still need to compile it before writing it to the DOM: $tempateCache检索模板后,您仍需要在将其写入DOM之前对其进行编译:

AngularJS: $compile AngularJS:$ compile

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

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