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