Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-bind-html directive removes style attribute when renders

Tags:

html

angularjs

When I write

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

where

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

angular removes style attribute, so image has initial size. What do you think? How I can avoid this?

Thanks!

like image 888
bbonch Avatar asked Mar 15 '14 11:03

bbonch


People also ask

How does ng-bind-HTML work?

The AngularJS ng-bind-html directive is used to bind content to an HTML element securely. It evaluates the expressions and inserts the resulting HTML into the element in a secure way. By default, the resulting HTML content will be sanitized using the $sanitize service.

Does ng-bind bind the application data to HTML tags in AngularJS?

ng-bind directive binds the AngularJS Application data to HTML tags. ng-bind updates the model created by ng-model directive to be displayed in the html tag whenever user input something in the control or updates the html control's data when model data is updated by controller.

Which directive is used to render contents of a variable as HTML in safe manner?

The ng-bind-html directive is a secure way of binding content to an HTML element.

How do you bind in HTML?

Purpose. The html binding causes the associated DOM element to display the HTML specified by your parameter. Typically this is useful when values in your view model are actually strings of HTML markup that you want to render. If you know your view model value is plain text, use the more efficient text binding instead.


2 Answers

Note that ng-bind-html-unsafe has been removed from Angular. I would rather create a filter, instead of adding a function to scope in order to avoid scopes pollution and improve the code reusability:

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

There is no need to write anything in the scope, just add the filter on the template:

<div ng-bind-html="resultList.section | unsafe"></div>
like image 182
Tommaso Sebastianelli Avatar answered Sep 18 '22 14:09

Tommaso Sebastianelli


use ng-bind-html="trustedHtml(resultList.section)" in html tag and put this function controller

$scope.trustedHtml = function (plainText) {
      return $sce.trustAsHtml(plainText);
}
<div ng-bind-html="trustedHtml(resultList.section)"></div>
like image 32
Nikhil Vibhani Avatar answered Sep 19 '22 14:09

Nikhil Vibhani