AngularJS 1.2.0 ngBindHtml和trustAsHtml不ngModel工作 [英] AngularJS 1.2.0 ngBindHtml and trustAsHtml not working with ngModel

查看:367
本文介绍了AngularJS 1.2.0 ngBindHtml和trustAsHtml不ngModel工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这应该是因为我有它的角1.0.8使用ngBindHtmlUnsafe完美的工作很容易。我读了API文档和计算器,我需要使用 $ sce.trustAsHtml() ngBindHtml ,但现在我似乎无法得到它的工作。

I feel like this should be really easy since I had it working perfectly with Angular 1.0.8 using ngBindHtmlUnsafe. I read on the API docs and on StackOverflow that I need to use $sce.trustAsHtml() with ngBindHtml now but I cannot seem to get it to work.

这基本上是我使用提供的格式我读

This is basically the format I am using given what I read:

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>

我认为这将是这个简单,但我一定是错的,失去了一些东西。

I thought it would be this straightforward but I must be wrong and missing something.

我放弃了这个简单的例子,一个Plunker: http://plnkr.co/编辑/ ZX4dONBlzv1X8BcO1IBV?p = preVIEW

I dropped this simple example to a Plunker: http://plnkr.co/edit/ZX4dONBlzv1X8BcO1IBV?p=preview

推荐答案

这就是你要找的是什么?
http://plnkr.co/edit/IZkzsuKHvbYiyV07CGqp

Is this what you're looking for? 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:
    
    

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>

这篇关于AngularJS 1.2.0 ngBindHtml和trustAsHtml不ngModel工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆