ng-bind-html过滤了style,怎么解决

ng-bind-html过滤了style,怎么解决,第1张

ng-bind-html过滤了style的解决方法是引入$sce模块,具体使用如下:

$scope.docHtml= $sce.trustAsHtml(data)

这样就可以将值转换为特权所接受并能安全地使用“ng-bind-html”了。

1、完整代码使用如下:

<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>

<script src="http://apps.bdimg.com/libs/angular.js/1.5.0-beta.0/angular-sanitize.min.js"></script>

<body>

<div ng-app="myApp" ng-controller="myCtrl">

    <p ng-bind-html="myText"></p>

</div>

<script>

var app = angular.module("myApp", ['ngSanitize'])

app.controller("myCtrl", function($scope) {

    $scope.myText = "My name is: <h1>John Doe</h1>"

})

</script>

2、运行结果:

用{{}}是一定没法输出html的

不知道你项目里是怎么限制的不能使用 ng-bind-html

那你试试自己写个指令来实现一下好了

var app = angular.model('app' , [])

app.directive('stringHtml' , function(){

  return function(scope , el , attr){

    if(attr.stringHtml){

      scope.$watch(attr.stringHtml , function(html){

        el.html(html || '')//更新html内容

      })

    }

  }

})

app.controller('TestCtrl' , function($scope){

  $scope.string = '<button type="button">测试html按钮</button>'

})

html:

<div ng-controller="TestCtrl">

  <span string-html="string"></span>

</div>

C#

ASP.Net

的aspx页面中

Eval()

单向绑定,用于显示

Bind()

双向绑定,可用于显示和更新


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8323976.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-15
下一篇2023-04-15

发表评论

登录后才能评论

评论列表(0条)

    保存