无法使用ng-repeat和ng-bind-html呈现原始HTML

时间:2014-12-12 14:35:43

标签: javascript html angularjs angular-ui-router

我需要插入原始html才能查看as is

<table>
<tr ng-repeat="el in tableRowsHTML track by $index" ng-bind-html="el">
                        {{el}}
 </tr>
 </table>

app.controller('Controller', function ($scope, promiseTemplateObj, $sce) {
$scope.tableRowsHTML = [];
$scope.generateTableRows = function(){ ....};

 $sce();
    $sce.trustAsHtml($scope.tableRowsHTML[1]);
    $sce.trustAsHtml($scope.tableRowsHTML[2]);
    $sce.trustAsHtml($scope.tableRowsHTML[3]);

如何解决?

更新

我明白了partially working with ui-router但问题仍然存在。

2 个答案:

答案 0 :(得分:1)

<tr ng-repeat="el in tableRowsHTML track by $index" ng-bind-html="el | toTrusted"></tr>

过滤(仅举例):

appRoot.filter('toTrusted', ['$sce', function ($sce) {
   return function (text) {
      return text ? $sce.trustAsHtml(text.replace(/\\n/g, '<br/>')) : '';
   };
}]);

appRoot - 您的应用程序。

更新:示例http://jsfiddle.net/c5j92821/

答案 1 :(得分:0)

您也可以查看ng-sanitize。这将帮助您将html从控制器绑定到正文。并在这个页面上查看examples

中的少数几个