【问题标题】:ng-bind-html with $sce not rendering html带有 $sce 的 ng-bind-html 不呈现 html
【发布时间】:2018-03-06 12:34:45
【问题描述】:

我从我的 Api 中获取了一个包含文本和 html 的字符串,我希望它同时呈现两者。当前仅呈现文本。这是我的视图和控制器:

控制器:

angular.module('app').controller('pakningInfohjelpModalController', pakningInfohjelpModalController); 

pakningInfohjelpModalController.$inject = ['$uibModalInstance', '$uibModal', 'content', '$sce' ]; 

function pakningInfohjelpModalController($uibModalInstance, $uibModall, content, $sce) {
    var ctrl = this; 

    ctrl.$onInit = () => {
        ctrl.title = content.modalTitle;
        content.modalBody = $sce.trustAsHtml(content.modalBody);
        if(content.modalBody == ''){
            ctrl.body = "Det finnes for øyeblikket ikke noen informasjon om dette feltet";
        }else{
            ctrl.body = content.modalBody.$$unwrapTrustedValue();
            console.log(ctrl.body);
        }
    }

    ctrl.lukk = () => {
        $uibModalInstance.close();
    }
}

查看:

<div class="modal-body">
        <h4>Hjelp for {{$ctrl.title}}</h4>
        <div class="card-group">
                <div class="card-body">
                    <p><div ng-bind-html="$ctrl.body"></div></p>
                </div>
                <pre>
                    {{$ctrl.body}}
                </pre>
        </div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-green pull-left" type="button" ng-click="$ctrl.lukk()">Lukk</button>
    </div>

控制器中的控制台日志同时记录文本和 html。我看过几个类似的问题,但解决方案不适用于我的问题。

资源:

Why is ng-bind-html not displaying anything?

文档也无济于事:

https://docs.angularjs.org/api/ng/directive/ngBindHtml

这是我要绑定的字符串:

                Fyllingsgraden skal indikere hvor godt innholdet i emballasjen opptar av emballasjens totale volum, basert på registrert bredde, dybde og høyde som ytre volum. <p><embed src="***" autostart="false"  width="100%" /></p>

源代码已删除,但在我的代码中

【问题讨论】:

  • 你能重现你的问题吗
  • embed.plnkr.co/hno8NpqdCjaP2zzdacoU 我在这个 plunkr 示例中粘贴了字符串。 html 没有呈现。
  • 如果你能做一个 plnker 或一个 jsfiddle 就好了
  • 查看我在上面评论中提供的链接。当我粘贴要绑定的字符串时,我只得到文本而不是 html。

标签: javascript angularjs


【解决方案1】:

使用ngSanitize,将其注入到模块中。

修改html代码如下。

<pre>
     <div ng-bind-html="$ctrl.body"></div>  
 </pre>

要了解更多关于ngSanitize 的信息,请查看this

【讨论】:

  • 它只显示字符串和html,没有渲染任何内容。
  • 我已将字符串粘贴到您提供的链接的演示中。 html 仍未呈现。
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多