javascriptangularjsui-codemirror

Changing option in ui-codemirror


I am trying to use ui-codemirror, and has the following code: JSBin:

<!DOCTYPE html>
<html>
<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.6.4/angular.min.js"></script>
  <script src="https://codemirror.net/lib/codemirror.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-codemirror/0.3.0/ui-codemirror.js"></script>
</head>
<body ng-app="YourApp"> 
  <div ng-controller="YourController">
    <div ng-model='testCM3' ui-codemirror='{{option}}'></div>
    {{option}}<br/>
    <a href="#" ng-click="changeOption()">changeOption</a>
  </div>
  <script type="text/javascript">     
    angular.module('YourApp', ['ui.codemirror'])
    .controller('YourController', ['$scope', function ($scope) {
      $scope.option = "{ lineNumbers: true }"
      $scope.testCM3 = "testCM3";
      $scope.changeOption = function () {
        if ($scope.option === "{ lineNumbers: true }") $scope.option = "{ lineNumbers: false }"
        else $scope.option = "{ lineNumbers: true }"
      }
    }])
  </script> 
</body>
</html>

By clicking on changeOption I would expect the lineNumbers to be hidden/shown. We could see that the value of option changes, however the codemirror does not change. Does anyone know what's wrong with my code?


Solution

  • I found it:

    1. ui-codemirror='{{option}}' should be written as ui-codemirror='option'
    2. option should be defined as an object rather than a string

    JSBin