2016-03-02 81 views
0

我已经查找了如何在angularJS中呈现html标记的类似问题,但是他们都没有为我工作,因为我正在使用ng-repeat。任何人都有任何建议。从AngularJS对象中呈现html标记

 <div ng-repeat="o in SurveyResults.SurveyQuestions"> 
        {{o.SurveyQuestionId}} 
        {{o.QuestionText}} 
    </div> 

o.QuestionText有数据如<b>How well do we deliver on our promises?</b> <br />Consider our responsiveness to your requests, ability to meet deadlines and <i>accountability for the outcomes</i>.

它显示的标签,而无需渲染的标签。

我曾尝试把这个在我的JS文件

homePage.directive('htmlSafe', function($parse, $sce) { 
    return { 
     link: function(scope, elem, attr) { 
      var html = attr.ngBindHtml; 
      if(angular.isDefined(html)) { 
       var getter = $parse(html); 
       var setter = getter.assign; 
       setter(scope, $sce.trustAsHtml(getter(scope))); 
      } 
     } 
    }; 
}); 

然后设置HTML绑定在重复这样

<div ng-repeat-html="o in SurveyResults.SurveyQuestions"> 
        {{o.SurveyQuestionId}} 
        {{o.QuestionText}} 
    </div> 

任何其他意见?

回答

1

只需使用NG绑定,HTML,你不需要新指令

<div ng-repeat="o in SurveyResults.SurveyQuestions"> 
       {{o.SurveyQuestionId}} 
       <span ng-bind-html="o.QuestionText"></span> 
</div>