javascript - ng-bind-html doesn't work for ng-options - Stack Overflow

I need to sanitize special characters in options, but it doesn't work correctly. Maybe anybody can

I need to sanitize special characters in options, but it doesn't work correctly. Maybe anybody can tell me how i should do it correctly?

For example:

HTML:

<div ng-controller="Ctrl">
    <select id="limitType" name="limit" ng-model="selectedLimit" ng-options="limit.text for limit in limits" ng-init="selectedLimit='5'" ng-bind-html="limit.text"></select>
<div>

JS:

var app = angular.module('app', ['ngSanitize']);

function Ctrl($scope) {
  $scope.limits = [{
    text: 'Afficher &#0153; par page'
  }, {
    text: 'Afficher 10 par page'
  }, {
    text: 'Afficher 15 par page'
  }, {
    text: 'Afficher 20 par page'
  }];   
}

Here is link on fiddle: /

I need to sanitize special characters in options, but it doesn't work correctly. Maybe anybody can tell me how i should do it correctly?

For example:

HTML:

<div ng-controller="Ctrl">
    <select id="limitType" name="limit" ng-model="selectedLimit" ng-options="limit.text for limit in limits" ng-init="selectedLimit='5'" ng-bind-html="limit.text"></select>
<div>

JS:

var app = angular.module('app', ['ngSanitize']);

function Ctrl($scope) {
  $scope.limits = [{
    text: 'Afficher &#0153; par page'
  }, {
    text: 'Afficher 10 par page'
  }, {
    text: 'Afficher 15 par page'
  }, {
    text: 'Afficher 20 par page'
  }];   
}

Here is link on fiddle: http://jsfiddle/rfTV2/3/

Share Improve this question asked Jan 17, 2014 at 13:34 MalMal 631 silver badge5 bronze badges 2
  • you need to add angular-sanitise.js to "External resources". Look here – BuDen Commented Jan 17, 2014 at 13:43
  • If you open attached link then you can see, that i added sanitise via option for angularjs-1.0.3. BTW, when i include include angularjs-1.2 and add angular-sanitise.js in "External resources" it still will not work. – Mal Commented Jan 17, 2014 at 14:29
Add a ment  | 

3 Answers 3

Reset to default 6

You have three options.

  1. You can include unicode characters in your source directly
  2. You can convert the html entities to unicode in the browser using JavaScript
  3. Or you can fall-back to ng-repeat and use ng-bind-html on your option tag.

I think CAT already provided answer. I am just providing readigs for how to use ng-bind-html

  1. ng-bind-html directive
  2. ng-sanitize module
  3. $sce service

Have you tried ?

$sce.trustAsHtml()

In your example it would be something like this (not tested)

function Ctrl($scope, $sce) {
  $scope.limits = [{
    text: $sce.trustAsHtml('Afficher &#0153; par page')
  }, {
    text: 'Afficher 10 par page'
  }, {
    text: 'Afficher 15 par page'
  }, {
    text: 'Afficher 20 par page'
  }];   
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744400788a4572374.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信