【问题标题】:Dynamic content with AngularJS and prettyprint使用 AngularJS 和 prettyprint 的动态内容
【发布时间】:2019-08-08 05:01:53
【问题描述】:

有一些代码块<pre>...</pre> 具有由 angularJS 提供的动态内容,必须打印得很漂亮。当内容更新时,htmlview 中既有旧的,也有新的(下次更新时 - 三个,依此类推..)

html

<div ng-repeat="(index, issue) in issues track by $index">
  . . .
  <div>
    <pre class="prettyprint linenums">{{issue.code}}</pre>
  </div>
  . . .
</div>

这是不正确的..每个问题都应该只有它自己的代码..

【问题讨论】:

    标签: javascript angularjs pretty-print


    【解决方案1】:

    通过将内容包装到&lt;div&gt; 并使用$sce 解决了这个问题:

    html

    <div ng-repeat="(index, issue) in issues track by $index">
      . . .
      <div ng-bind-html="getCode(issue.code)"></div>
      . . .
    </div>
    

    控制器

    app.controller('appCtrl', ['$scope', '$sce', function(scope, sce) {
      ...
      scope.getCode = function(code) {
        // console.log(code);
        return sce.trustAsHtml("<pre class='prettyprint linenums'>" + code + "</pre>");
      };
      ...
    }
    

    每次内容更新后都应该调用漂亮的打印方法

    PR.prettyPrint(); // google-code-prettify
    

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2017-10-27
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多