Error: uiCodemirror3 can only be applied to a textarea element

Error: uiCodemirror3 can only be applied to a textarea element

根据 ui-codemirror 的文档:

The ui-codemirror directive plays nicely with ng-model.

The ng-model will be watched for to set the CodeMirror document value (by setValue).

The ui-codemirror directive stores and expects the model value to be a standard javascript String.

但是,我的以下代码 (JSBin) returns 在控制台中出错 Error: uiCodemirror3 can only be applied to a textarea element

<html ng-app="flapperNews">
<head>
  <link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css">
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  <script src="https://codemirror.net/lib/codemirror.js"></script>
  <script src="https://codemirror.net/mode/xml/xml.js"></script>
  <script src="https://codemirror.net/mode/htmlmixed/htmlmixed.js"></script>
  <script src="https://codemirror.net/mode/javascript/javascript.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.js"></script>
  <script>
    var app = angular.module('flapperNews', ['ui']);
    app.controller('MainCtrl', ['$scope', function ($scope) {
      $scope.x = "<html><body>abc</body></html>";
    }])
  </script>
</head>
<body ng-controller="MainCtrl">
  <div ui-codemirror ng-model="x"></div>
</body>
</html>

有谁知道如何正确使用ui-codemirror 指令

只需将 div 替换为 textarea 就像下面的代码 ( jsbin )

<textarea ui-codemirror ng-model="x"></textarea>

您可以在以下位置找到错误消息 https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.js 但是 https://github.com/angular-ui/ui-codemirror/blob/master/src/ui-codemirror.js 没有。

因此您可以等待 angular-ui 更新它或单独使用 ui-codemirror 进行最后更改。

您可以添加以下库

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-codemirror/0.3.0/ui-codemirror.js"></script>

并删除以下内容

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.js"></script> 

angular-ui.js 有旧的代码镜像指令

并注入

var app = angular.module('flapperNews', ['ui.codemirror']);

示例JSBin