2016-11-28 9 views
1

私はMoodle WebServiceを使用してAngularJS Moodle Webアプリケーションを開発しました。私はMoodle eLearningのクイズを表示するためにトリンギングしています。JSON HTML文字列をHTMLに変換します

私はすべての質問を$http.を使って得ることができます。今の問題は、私は質問を取得するときに、すべての質問は、このようなHTMLコードが付属していることである:私はこれを使用してい

JSON Response

Anが応答(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; 
       }) 
}) 
を取得するためにcontrolores

次のコードを使用してHTMLに質問を表示すると、HTMLコードが文字列として取得され、ng-bind-htmlを使用しようとしましたが、エラーが発生しました。

With regular line

:私はこのライン {{question.html}}<br /><br />を使用する場合、私はこれを取得してい

JSON.stringify 
angular.fromJson(json); 

<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> 

Error

また、私が試しました

あなたの助けを借りてくれてありがとう!

+1

あなたの代わりに、角度「の 'angular.js' を使用した場合。 min.js 'リンクの代わりにエラーテキストが表示されます。 :) –

答えて

1

言うように私はあなたが厳格な文脈エスケープサービス$sce)が必要だと思います。 これは、O.Kであるコンテキストを指定できるサービスです。任意の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}} 
+0

それを 'ng-repeat'のように使うと動作しません。 – rfcabal

+0

O.K.この問題の解決方法については、この回答http://stackoverflow.com/questions/24459194/angularjs-using-sce-trustashtml-with-ng-repeatを参照してください。私の答えも更新されました。 – frishi

+0

私はこのフィルタ '.filter(" sanitize "、['$ sce'、function($ sce){ return function(htmlCode){ return $ sceを使用しなければなりません。trustAsHtml(htmlCode); } }]) 'ありがとうございました! – rfcabal

1

あなたはバインディングのhtmlを機能させるために$sceサービス

$sce.trustAsHtml(varWithHTML) 

を使用する必要があります。

ドキュメントはhttps://docs.angularjs.org/api/ng/service/ $のSCE

+0

私は '$ scope.questions2 = $ sce.trustAsHtml(response.data.questions [0] .html)'のような特定のものを試しましたが、 '、' $ scope.questions = response.dataで試してみました。質問 '、ng-repeat'は機能しません。 – rfcabal

関連する問題