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!
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.
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.
The ng-bind-html directive is a secure way of binding content to an HTML element.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With