javascript - 具有 HTML 格式的 AngularJS 动态行生成

标签 javascript jquery html css angularjs

我正在使用 ng-repeat 在 HTML 上生成表格行。我的行内容中有 HTML 标记,我想用它来格式化内容。但它只会将带有 html 标记的文本打印为纯文本,而不会将它们识别为 HTML 标记。

<tr ng-repeat=" styleRowDetail in styleRowDetails">
  <td>{{styleRowDetail.shortMessage}}
  </td>
  <td>{{styleRowDetail.classOriginated}}
  </td>
  <td>{{styleRowDetail.method}}
  </td>
  <td>{{styleRowDetail.lineRange}}
  </td>
  <td>{{styleRowDetail.details}}
  </td>
</tr>

This is what my HTML output looks like I want to get the HTML tags in column 3 to be applied to the text.

最佳答案

这是一个工作代码。

var jsondata = [{
  "shortMessage": "data 1",
  "classOriginated": "data 2",
  "method": "<i>data 3</i>",
  "lineRange": "data 4",
  "details": "<b>data 5</b>"
}];

var app = angular.module('myApp', ['ngSanitize']);
app.controller('MyCtrl', function($scope, $sce) {
  $scope.styleRowDetails = jsondata;
});
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-sanitize.js"></script>
<script src="controller.js"></script>
<h3>Data</h3>
<div ng-app="myApp" ng-controller="MyCtrl">
  <table>
    <tr ng-repeat=" styleRowDetail in styleRowDetails">
      <td ng-bind-html="styleRowDetail.shortMessage">
      </td>
      <td ng-bind-html="styleRowDetail.classOriginated">
      </td>
      <td ng-bind-html="styleRowDetail.method">
      </td>
      <td ng-bind-html="styleRowDetail.lineRange">
      </td>
      <td ng-bind-html="styleRowDetail.details">
      </td>
    </tr>
  </table>

</div>

希望对您有所帮助:)

关于javascript - 具有 HTML 格式的 AngularJS 动态行生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37772779/

相关文章:

javascript - 使用 Javascript Reduce 添加一个属性并乘以另一个属性

javascript - jQuery:wrapInner 与父元素的 href

javascript - 在 div 标签附近出现错误

javascript - 使用 IDE 工具创建网站并在 Web 服务器上进行测试

javascript - 如何使用vuejs确保多个选项卡之间的变量一致性

jquery从iframe内容访问iframe id

html - 在 Bootstrap 中更改行高

javascript - 一旦第三方脚本加载失败,立即运行一段 JavaScript

javascript - 单击突出显示文本(javascript jquery html)

javascript - jquery 中的动态容器名称