加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS 1.2.0 ngBindHtml和trustAsHtml不能与ngModel一起使用

发布时间:2020-12-17 17:05:32 所属栏目:安全 来源:网络整理
导读:我觉得这应该很容易,因为我使用ngBind HtmlUnsafe与Angular 1.0.8完美配合.我在API文档和StackOverflow上阅读了我现在需要使用带有ngBindHtml的$sce.trustAsHtml(),但我似乎无法让它工作. 这基本上是我在阅读的内容中使用的格式: var myApp = angular.modul
我觉得这应该很容易,因为我使用ngBind HtmlUnsafe与Angular 1.0.8完美配合.我在API文档和StackOverflow上阅读了我现在需要使用带有ngBindHtml的$sce.trustAsHtml(),但我似乎无法让它工作.

这基本上是我在阅读的内容中使用的格式:

var myApp = angular.module('myApp',[]);

function myController($scope,$sce){
    $scope.myHtml = $sce.trustAsHtml($scope.sourceText);
}

HTML:

<html ng-app="myApp">

  <head>
    <script data-require="angular.js@1.2.0-rc3" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-controller="myController">
      <textarea ng-model="sourceText"></textarea>

      <div ng-bind-html="myHtml"></div>
    </div>
  </body>

</html>

我认为这将是直截了当的,但我一定是错的,错过了一些东西.

我把这个简单的例子放到了Plunker:http://plnkr.co/edit/ZX4dONBlzv1X8BcO1IBV?p=preview

解决方法

这是你在找什么?
http://plnkr.co/edit/IZkzsuKHvbYiyV07CGqp

// would strongly suggest including sanitize in your scripts and injecting it
// into your app here to prevent "unsafe as safe" errors
var myApp = angular.module('myApp',['ngSanitize']);

myApp.controller('myController',['$scope','$sce',function myController($scope,$sce){
  $scope.myHtml = "initial";  //not needed,for testing

  $scope.changeText = function() {
    $scope.myHtml = $sce.trustAsHtml($scope.sourceText);
  }
}]);

HTML:
????
????

<head>
    <script data-require="angular.js@1.2.0-rc3" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>
    <script src="http://code.angularjs.org/1.2.0-rc.3/angular-sanitize.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-controller="myController">
      <textarea ng-model="sourceText" ng-change="changeText()"></textarea>

      <div ng-bind-html="myHtml"></div>
    </div>
  </body>

</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读