Search code examples
angularjscodemirrorui-codemirror

Error: uiCodemirror3 can only be applied to a textarea element


According to the doc of 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.

However, my following code (JSBin) returns an error Error: uiCodemirror3 can only be applied to a textarea element in the console.

<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>

Does anyone know how to correctly use ui-codemirror directive?


Solution

  • You can add the following libraries

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

    and remove the following

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

    angular-ui.js has old codemirror directives

    and inject

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

    Example JSBin