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

前端 未结 10 1460
别跟我提以往
别跟我提以往 2020-11-22 03:51

ng-bind-html-unsafe was removed in Angular 1.2

I\'m trying to implement something where I need to use ng-bind-html-unsafe. In the docs and

相关标签:
10条回答
  • 2020-11-22 04:18

    Simply creating a filter will do the trick. (Answered for Angular 1.6)

    .filter('trustHtml', [
            '$sce',
            function($sce) {
                return function(value) {
                    return $sce.trustAs('html', value);
                }
            }
        ]);
    

    And use this as follow in the html.

    <h2 ng-bind-html="someScopeValue | trustHtml"></h2>
    
    0 讨论(0)
  • 2020-11-22 04:20

    If you want the old directive back, you can add this to your app:

    Directive:

    directives.directive('ngBindHtmlUnsafe', ['$sce', function($sce){
        return {
            scope: {
                ngBindHtmlUnsafe: '=',
            },
            template: "<div ng-bind-html='trustedHtml'></div>",
            link: function($scope, iElm, iAttrs, controller) {
                $scope.updateView = function() {
                    $scope.trustedHtml = $sce.trustAsHtml($scope.ngBindHtmlUnsafe);
                }
    
                $scope.$watch('ngBindHtmlUnsafe', function(newVal, oldVal) {
                    $scope.updateView(newVal);
                });
            }
        };
    }]);
    

    Usage

    <div ng-bind-html-unsafe="group.description"></div>
    

    Source - https://github.com/angular-ui/bootstrap/issues/813

    0 讨论(0)
  • 2020-11-22 04:20
    my helpful code for others(just one aspx to do text area post)::
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication45.WebForm1" %>
    
    <!DOCTYPE html>
    
        enter code here
    
    <html ng-app="htmldoc" xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="angular.min.js"></script>
        <script src="angular-sanitize.min.js"></script>
        <script>
            angular.module('htmldoc', ['ngSanitize']).controller('x', function ($scope, $sce) {
                //$scope.htmlContent = '<script> (function () { location = \"http://moneycontrol.com\"; } )()<\/script> In last valid content';
                $scope.htmlContent = '';
                $scope.withoutSanitize = function () {
                    return $sce.getTrustedHtml($scope.htmlContent);
                };
                $scope.postMessage = function () {
                    var ValidContent = $sce.trustAsHtml($scope.htmlContent);
    
                    //your ajax call here
                };
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            Example to show posting valid content to server with two way binding
            <div ng-controller="x">
                <p ng-bind-html="htmlContent"></p>
                <textarea ng-model="htmlContent" ng-trim="false"></textarea>
                <button ng-click="postMessage()">Send</button>
            </div>
        </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 04:21

    JavaScript

    $scope.get_pre = function(x) {
        return $sce.trustAsHtml(x);
    };
    

    HTML

    <pre ng-bind-html="get_pre(html)"></pre>
    
    0 讨论(0)
提交回复
热议问题