在ui-codemirror中更改选项

时间:2017-08-09 16:59:07

标签: javascript angularjs ui-codemirror

我正在尝试使用ui-codemirror,并且代码如下: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>

点击changeOption我希望隐藏/显示lineNumbers。我们可以看到option的值发生了变化,但是代码镜像没有变化。有谁知道我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我找到了它:

  1. ui-codemirror='{{option}}'应写为ui-codemirror='option'
  2. option应定义为对象而不是字符串
  3. JSBin