[英]Convert JSON HTML string to HTML
我已经使用Moodle WebService开发了AngularJS Moodle网络应用程序,正在尝试展示Moodle电子学习中的测验。
我可以使用$http.
来获取每个问题$http.
。 现在的问题是,当我收到问题时,每个问题都带有这样的HTML代码:
我正在使用此controlores来获取响应(url5)
app.controller('quizCtrl', function($rootScope, $scope, $http) {
url4 = concatUrl + 'mod_quiz_start_attempt' + '&quizid=2';
$http.get(url4).then(function (response) {
//console.log(response.data);
})
url5 = concatUrl + 'mod_quiz_get_attempt_data' + '&attemptid=7&page=0';
$http.get(url5).then(function (response) {
console.log(response.data);
$scope.questions = response.data.questions;
})
})
当我使用以下代码在HTML中显示问题时,我将HTML代码作为字符串获取,并尝试使用ng-bind-html
但出现错误。
<div role="main" id="main" class="ui-content scroll" ng-app="mainApp">
<div data-role="content" class="pane" id="">
<div class="page-wrap scroll" ng-controller="quizCtrl">
<div ng-repeat="question in questions | orderBy : 'question.number'" id="{{question.number}}">
<div>
<!--{{question.html}}<br /><br />-->
<p ng-bind-html="question.html"> </p><br /><br />
</div>
</div>
</div>
</div>
另外,我尝试了:
JSON.stringify
angular.fromJson(json);
当我使用这些行{{question.html}}<br /><br />
,我得到的是:
感谢您的帮助!
您需要使用$sce
服务
$sce.trustAsHtml(varWithHTML)
使绑定的HTML工作。
正如文档所说的https://docs.angularjs.org/api/ng/service/ $ sce
我认为您需要严格的上下文转义服务 ( $sce
)。 这是一项服务,使您可以指定可以允许任意HTML的上下文。
文件: https : //docs.angularjs.org/api/ng/service/ $ sce
将其注入您的控制器中:
app.controller('quizCtrl', function($rootScope, $scope, $http, $sce)
...
$http.get(url5).then(function (response) {
console.log(response.data);
$sce.trustAsHtml = $sce.trustAsHtml; // <- needs to be exposed on $scope
$scope.questions = $sce.trustAsHtml(response.data.questions);
})
...
在您看来:
{{questions}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.