zoukankan      html  css  js  c++  java
  • 如何Angularjs1.3在页面中输出带Html标记的文本

    基于安全考虑,Angularjs不允许用ng-bind或者{{}}的方法输出html文本。

    在实际的应用中,比如信息管理系统,用在线编辑器编辑出来的文章都带有html标记,这种情况下可以用ng-bind-html将其输出到前台页面。

    1、在前台页面中包含sanitize.js

    <script type="text/javascript" src="webjars/angular-sanitize/1.3.11/angular-sanitize.min.js"></script> 

    2、在mode、和controller增加对html文本的安全过滤

    angular.module('scenceApp',['ui.router','ngResource','ngSanitize','restangular'])
    .controller('scenceViewController',function($scope,Restangular,$stateParams, $sce){
        Restangular.one("scences",$stateParams.id).get().
         then(function(data) {
            $scope.scence = data;
            $scope.scence.info = $sce.trustAsHtml(data.info); //对info字段进行安全过滤
         });
    })

    3、在前台页面中用ng-bind-html绑定

    <div ng-bind-html="scence.info"></div>
  • 相关阅读:
    字符串系列复习
    点分治总结
    LCT总结
    网络流总结
    centOS7下安装GUI图形界面
    周记 2014.10.8
    周记 2014.9.28
    周记 2014.9.20
    tar命令
    [转]bit与byte
  • 原文地址:https://www.cnblogs.com/mingziday/p/4824899.html
Copyright © 2011-2022 走看看