最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ng-bind-html html with css does not work - Stack Overflow

programmeradmin1浏览0评论
<ion-item ng-bind-html="renderHtml(word[key])">

</ion-item>

Where word[key] is

<ul>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
</ul>

CSS:

ul {padding: 1px 10px 1px 20px;list-style-type: upper-roman;}

I have a much more plex structure with css. The CSS works on cordova and react but since i am porting function to ng, the css do not work. Please Advise or point what i am missing here.

<ion-item ng-bind-html="renderHtml(word[key])">

</ion-item>

Where word[key] is

<ul>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
</ul>

CSS:

ul {padding: 1px 10px 1px 20px;list-style-type: upper-roman;}

I have a much more plex structure with css. The CSS works on cordova and react but since i am porting function to ng, the css do not work. Please Advise or point what i am missing here.

Share Improve this question edited Mar 9, 2015 at 20:06 Dawson Loudon 6,0392 gold badges29 silver badges31 bronze badges asked Mar 9, 2015 at 17:34 moeen-ud-Dinmoeen-ud-Din 1701 silver badge13 bronze badges 1
  • 1 can you create a plnkr? plnkr.co/edit/?p=catalogue – dowomenfart Commented Mar 9, 2015 at 17:52
Add a ment  | 

1 Answer 1

Reset to default 5

You're seeing the angular XSS security in play (see http://errors.angularjs/1.2.23/$sce/unsafe).

There are 2 ways to fix this:

  1. If the html is user created/influenced in some way, you will need to include the $sanitize module. Refer to the link above.

  2. If the html is from you only, mark it as trusted HTML using $sce.trustAsHtml. Something like:

angular.module('myapp', []).controller('myctrl', function($scope, $sce) {
  $scope.renderHtml = function(html) {
    return html;
  };
  $scope.word = $sce.trustAsHtml('\
<ul>\
  <li>item 1</li>\
  <li> item 2 </li>\
  <li>item 3</li>\
</ul>');
});
ul {padding: 1px 10px 1px 20px;list-style-type: upper-roman;}
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myctrl">
  <ion-item ng-bind-html="word"></ion-item>
</div>

发布评论

评论列表(0)

  1. 暂无评论