带有样式的 ng-bind-html 不起作用

And*_*rej 3 angularjs


我正在寻找如何使用ng-bind-html 作为输入来解决我的问题,例如我收到了一个带有 html 的图标

icon: '<i class="fa fa-sign-out"><i>'
Run Code Online (Sandbox Code Playgroud)

这适用于ng-bind-html:

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

显示是这样的:

在此处输入图片说明

但比我尝试为 fa-icon 添加红色

icon: '<i class="fa fa-sign-out" style="color:red;"><i>'
Run Code Online (Sandbox Code Playgroud)

但没有成功

所以我查看了 HTML ,似乎style="color:red;"不在那里。

但我不会这样显示:

(我尝试在开发工具中添加样式以查看它是否有效)

在此处输入图片说明

你好

Man*_*ham 5

使用$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);
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)