ng-bind-html指令在渲染时删除样式属性

bbo*_*nch 10 html angularjs

当我写作

 <div ng-bind-html="slideContent"></div>
Run Code Online (Sandbox Code Playgroud)

哪里

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

angular删除样式属性,因此图像具有初始大小.你怎么看?我怎么能避免这个?

谢谢!

小智 8

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

app.filter('unsafe', ['$sce', function ($sce) {
    return function (input) {
        return $sce.trustAsHtml(input);
    }
}]);
Run Code Online (Sandbox Code Playgroud)

不需要在scope中写任何东西,只需在模板上添加过滤器:

<div ng-bind-html="resultList.section | unsafe"></div>
Run Code Online (Sandbox Code Playgroud)


小智 5

在html标记中使用ng-bind-html =“ trustedHtml(resultList.section)”并将此功能控制器放入

$scope.trustedHtml = function (plainText) {
      return $sce.trustAsHtml(plainText);
}
Run Code Online (Sandbox Code Playgroud)
<div ng-bind-html="trustedHtml(resultList.section)"></div>
Run Code Online (Sandbox Code Playgroud)


Ham*_*med 0

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