Using HTML Entities within Angular strings

后端 未结 2 732
情歌与酒
情歌与酒 2020-12-03 21:09

Given a string in my $scope model which contains an HTML entity, how do I ensure that the entity is properly displayed as an HTML character rather than a literal string?

2条回答
  •  情深已故
    2020-12-03 21:58

    You do not need $sce to bind to strings with html. All you need is to inject ngSanitize into your app (it is not a core module), and then use the ng-bind-html attribute directive.

    JavaScript

    var app = angular.module('app', ['ngSanitize']);
    
    app.controller('MainCtrl', function($scope) {
        $scope.title = '½ Cookies & Cream';
    });
    

    Html

    Plunkr

提交回复
热议问题