如何使用角度js将动态内容显示到ck编辑器中

时间:2013-09-30 07:07:36

标签: javascript angularjs

ck editor text area
<textarea cols="100" id="editor1" name="editor1" rows="50" data-ng-model="report.reportlist">

</textarea>
<div>{{ report.reportlist }}</div>

我在div中获得了价值,但没有在ck编辑器中获得

我的控制器

$scope.report.reportlist = data ;

data = <p><h1>PRO/AH/EDR> African swine fever - Belarus (03): (HR) 1st rep, OIE, RFI</h1><br/><br/><p>African Swine Fever &mdash; Worldwide/Unknown<br/></p>

我不明白为什么它没有在CK编辑器中显示。 我正在使用角度js

1 个答案:

答案 0 :(得分:2)

它不起作用,因为CKEditor中的内容实际上不在textarea本身(textarea元素被隐藏)。要保持范围变量和CKeditor同步,您需要监听CKEditor事件并相应地更新范围变量。
我在这里做了一个快速演示:http://jsbin.com/iMoQuPe/2/edit

HTML:

<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div ng-controller="CkCtrl">
    <textarea name="editor" id="" cols="30" rows="10" ng-model="editorData"></textarea>
    <pre>
      {{ editorData }}
    </pre>
  </div>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.0.1/ckeditor.js"></script>
  <script>
    CKEDITOR.replace( 'editor' );
  </script>
</body>
</html>

JavaScript:

function CkCtrl($scope) {
  // Load initial data, doesn't matter where this comes from. Could be a service
  $scope.editorData = '<h1>This is the initial data.</h1>';

  var editor = CKEDITOR.instances.editor;

  // When data changes inside the CKEditor instance, update the scope variable
  editor.on('instanceReady', function (e) {
    this.document.on("keyup", function () {
      $scope.$apply(function () {
        $scope.editorData = editor.getData();
      });
    });
  });
}