我想将HTML传回网页,但它是以文字形式传递的。我有什么想法吗?
HTML:
<div ng-app="app" ng-controller="ctrl">
{{price}}
<br>
Only <strike>$49</strike> $29
</div>
角:
var app = angular.module('app',[]);
app.controller('ctrl', function($scope,$sce){
$scope.price = $sce.trustAsHtml('Only <strike>$'+49+'</strike> $'+29);;
});
输出:
Only <strike>$49</strike> $29
Only $49 $29
答案 0 :(得分:2)
以下是您的代码,其中添加了<span>
标记,其值为ng-bind-html
。我可能完全错了,因为我自己并不是最好的角度,但是我不相信你想要做的事情会像你想要的那样工作,因为Angular试图保持Sandbox模型在HTML注入方面相当严格
var app = angular.module('app', []);
app.controller('ctrl', function($scope, $sce) {
$scope.price = $sce.trustAsHtml('Only <strike>$' + 49 + '</strike> $' + 29);;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<span ng-bind-html="price"></span>
<br>Only <strike>$49</strike> $29
</div>