【问题标题】:ng-bind-html directive removes style attribute when rendersng-bind-html 指令在渲染时删除样式属性
【发布时间】:2014-04-20 19:51:02
【问题描述】:

当我写作时

 <div ng-bind-html="slideContent"></div>

在哪里

    this.$scope.slideContent = 
this.$sce.trustAsHtml("<img src='1.jpg' style='width: 231px'></img>");

angular 移除样式属性,因此图像具有初始大小。 你怎么看?我怎样才能避免这种情况?

谢谢!

【问题讨论】:

标签: html angularjs


【解决方案1】:

请改用ng-bind-html-unsafe。但请注意不要将其与用户提供的输入一起使用。

【讨论】:

    【解决方案2】:

    在 html 标签中使用 ng-bind-html="trustedHtml(resultList.section)" 并把这个功能控制器

    $scope.trustedHtml = function (plainText) {
          return $sce.trustAsHtml(plainText);
    }
    &lt;div ng-bind-html="trustedHtml(resultList.section)"&gt;&lt;/div&gt;

    【讨论】:

      【解决方案3】:

      请注意,ng-bind-html-unsafe 已从 Angular 中删除。 我宁愿创建一个过滤器,而不是在作用域中添加一个函数,以避免作用域污染并提高代码的可重用性:

      app.filter('unsafe', ['$sce', function ($sce) {
          return function (input) {
              return $sce.trustAsHtml(input);
          }
      }]);
      

      范围内不需要写任何东西,只需在模板上添加过滤器即可:

      <div ng-bind-html="resultList.section | unsafe"></div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-11
        • 1970-01-01
        • 2015-08-31
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 2017-01-04
        • 1970-01-01
        相关资源
        最近更新 更多