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

Rendering a template into a trusted HTML string [AngularJS]

本文关键字:HTML 字符串 AngularJS 信任      更新时间:2023-09-26

我正在使用在这里找到的AngularJS烤面包机来显示来自我的API的ModelState错误。您可以传递AngularJS烤面包机受信任的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 之前对其进行编译:

角度JS:$compile