AngularJS Binding和£符号

时间:2015-07-16 11:17:08

标签: angularjs data-binding

如果我直接在JSON数据中使用井号,我可以在以下示例中成功显示磅。如何判断我要显示的角度 Misko得到£

(function(angular) {
  'use strict';
angular.module('oneTimeBidingExampleApp', []).
  controller('EventController', ['$scope', function($scope) {
    var counter = 0;
    var names = ['Igor got �', 'Misko got £', 'Chirayu got £'];
    /*
     * expose the event object to the scope
     */
    $scope.clickMe = function(clickEvent) {
      $scope.name = names[counter % names.length];
      counter++;
    };
  }]);
})(window.angular);
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example28-production</title>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
  <script src="script.js"></script>
  
</head>
<body ng-app="oneTimeBidingExampleApp">
  <div ng-controller="EventController">
  <button ng-click="clickMe($event)">Click Me</button>
  <p id="one-time-binding-example">One time binding: {{::name}}</p>
  <p id="normal-binding-example">Normal binding: {{name}}</p>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

Angular默认是转义/清理,但您可以使用ng-bind-html指令,这样显示html:

<div ng-bind-html="'Misko got &pound;'"></div>

<div ng-bind-html="name"></div>

更多信息:https://docs.angularjs.org/api/ngSanitize/service/ $ sanitize

答案 1 :(得分:1)

将ngSanitize添加为模块的依赖项。还要在HTML中包含以下脚本。

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-sanitize.min.js"></script>

angular.module('oneTimeBidingExampleApp', ['ngSanitize'])

在你的html中使用ng-bind-html,以便angular将它显示为HTML而不是纯文本

<p id="normal-binding-example">Normal binding: <span ng-bind-html="name"></span></p>