How to display   or any raw html in angular data?

后端 未结 5 1575
后悔当初
后悔当初 2020-12-09 08:29

How can I display   as space not as string. Is there raw filter like in twig?

{{item}}
$scope.item
相关标签:
5条回答
  • 2020-12-09 08:51

    You can also create a filter for that.

    Filter

    app.filter("trustHtmlContent", ['$sce', $sce => htmlCode => $sce.trustAsHtml(htmlCode)]);
    

    usage

    <ANY ng-bind-html="value | trustHtmlContent"></ANY>
    

    src: How do you use $sce.trustAsHtml(string) to replicate ng-bind-html-unsafe in Angular 1.2+

    0 讨论(0)
  • 2020-12-09 09:02

    Try using ngBindHtml

    <div data-ng-bind-html="item"></div>
    
    0 讨论(0)
  • 2020-12-09 09:05

    You can use the ascii code for &nbsp in a string. Alt-255.

    This will correctly render as an &nbsp.

    0 讨论(0)
  • 2020-12-09 09:07

    For Angular 1.3.x version:

    Use the $sce service to mark the string as safe to use in a specific context (HTML in this case).
    See the documentation here

    HTML code:

    <div ng-app='myApp' ng-controller="Controller">
       <div ng-bind-html="item"></div>
    </div>
    

    JS code (controller):

    var app = angular.module('myApp', []);
    app.controller('Controller', function ($scope, $sce) {
       $scope.item = $sce.trustAsHtml('<span>Some HTML code</span>');
    });
    
    0 讨论(0)
  • 2020-12-09 09:08

    It can be easily done by using ngBindHtml

    For Angular above 1.2.x version:

    use ng-bind-html

    Working Demo

    html

    <div ng-app='myApp' ng-controller="Controller">
       <div ng-bind-html="item"></div>
    </div>
    

    script

    var app = angular.module('myApp', ['ngSanitize']);
    app.controller('Controller', function ($scope, $sce) {
       $scope.item = 'What&nbsp;Is&nbsp;Your&nbsp;Name?';
    });
    

    For Angular 1.0.x version:

    Working Demo

    use ng-bind-html-unsafe

    html

    <div ng-app='myApp' ng-controller="Controller">
       <div ng-bind-html-unsafe="item"></div>
    </div>
    

    script

    var app = angular.module('myApp', []);
    app.controller('Controller', function ($scope) {
       $scope.item = 'What&nbsp;Is&nbsp;Your&nbsp;Name?';
    });
    
    0 讨论(0)
提交回复
热议问题