我正在尝试使用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
的值发生了变化,但是代码镜像没有变化。有谁知道我的代码有什么问题?
答案 0 :(得分:0)
我找到了它:
ui-codemirror='{{option}}'
应写为ui-codemirror='option'
option
应定义为对象而不是字符串