Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-bind-html with style does not work

Tags:

angularjs


I'm searching how to solve my problem with ng-bind-html as input I recive for example an icon with html

icon: '<i class="fa fa-sign-out"><i>'

this works fine with ng-bind-html:

let buttonIcon = angular.element(`<span ng-bind-html="icon"></span>`);

and the display is like that:

enter image description here

but than I tried to add red color to fa-icon

icon: '<i class="fa fa-sign-out" style="color:red;"><i>'

but with no success

so I looked at HTML of and it seems that style="color:red;" is not in there.

but I wont to display like that:

(I tried add style in dev. tools to look if it works)

enter image description here

Greetings

like image 274
Andrej Avatar asked Aug 01 '26 20:08

Andrej


1 Answers

Use $sce.trustAsHtml..

https://docs.angularjs.org/api/ng/service/$sce

(function(angular) {
  'use strict';
angular.module('myApp', ['ngSanitize'])
  .controller('ctrl', ['$scope','$sce', function($scope,$sce) {

$scope.uCanTrust = function(string){
return $sce.trustAsHtml(string);
}
$scope.Stack = '<i class="fa fa-stack-overflow" style="color:#0077dd;font-size:34px" aria-hidden="true"></i>';
$scope.icon= $sce.trustAsHtml('<i class="fa fa-stack-overflow" style="color:red;font-size:34px" aria-hidden="true"></i>');
    $scope.trustme= $sce.trustAsHtml('<a href="#" style="color:red">Click Me!</a>');

    
  }]);
})(window.angular);
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
  <script src="//code.angularjs.org/snapshot/angular-sanitize.js"></script></head>
<body ng-app="myApp">
  <div ng-controller="ctrl">
 <p ng-bind-html="trustme"></p>
 <p ng-bind-html="icon"></p>
 
 <p ng-bind-html="uCanTrust(Stack)"></p>
</div>
</body>
</html>
like image 80
Manikandan Velayutham Avatar answered Aug 03 '26 12:08

Manikandan Velayutham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!