我觉得这应该很容易,因为我使用 ngBindHtmlUnsafe 让它与 Angular 1.0.8 完美配合。我阅读了我需要使用的 API 文档和 StackOverflow$sce.trustAsHtml()
with ngBindHtml
现在但我似乎无法让它发挥作用。
鉴于我读到的内容,这基本上是我使用的格式:
var myApp = angular.module('myApp', []);
function myController($scope, $sce){
$scope.myHtml = $sce.trustAsHtml($scope.sourceText);
}
html:
<html ng-app="myApp">
<head>
<script data-require="[email protected]" 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);
}
}]);
网页:
<head>
<script data-require="[email protected]" 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)