ngSanitize仍然显示文本而不是HTML

时间:2017-10-10 08:11:19

标签: javascript html angularjs

我想在AngularJS范围内有一些项目,我希望将其视为HTML,我发现需要将ngSanitize添加到应用程序才能执行此操作。所以我将它添加到索引页面上的声明中:

<script src="Scripts/angular-sanitize.min.js"></script>

并将其包含在应用

var app = angular.module("HtJobPortal", ["LocalStorageModule", "ngSanitize"]);
var serviceBase = "http://htauth.htcsol.local:65200/";
var resourceBase = "http://localhost:50915/";
(function () {
   "use strict";

    app.constant("ngAuthSettings", {
        apiServiceBaseUri: serviceBase,
        apiResourceBaseUri: resourceBase,
        clientId: "TMS_Portal"
    });
    app.config(function ($httpProvider) {
        $httpProvider.interceptors.push("authInterceptorService");
    });
    app.run(["authService", function (authService) {
        authService.fillAuthData();
    }]);

})();

然后将'ng'属性添加到我想要看作HTML的标签:

<div class="panel-body">
    <div class="col-lg-3 module text-center" ng-bind-html-unsafe="html">
        Jobs Module <br />
        {{settings.jobs}}
    </div>
    <div class="col-lg-3 module text-center" ng-bind-html-unsafe="html">
        Warehouse Module <br />
        {{settings.warehouse}}
    </div>
    <div class="col-lg-3 module text-center" ng-bind-html-unsafe="html">
        Stock Module <br />
        {{settings.stock}}
    </div>
    <div class="col-lg-3 module text-center" ng-bind-html-unsafe="html">
        Tracking Module <br />
        {{settings.tracking}}
    </div>
</div>

还有其他我想念的东西吗?

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的angularjs?从{1.2}版本中删除ng-bind-html-unsafe。如果要绑定html,可以使用ng-bind-html。但是您需要在角度范围内使用$sce.trustAsHtml(html)转换html字符串。