AngularJS
과 ui-codemirror
을 사용하여 매우 기본적인 운동장을 코딩하고 있습니다. 코드는 다음과 같습니다 (JSBin).컨트롤러에서 2 ui-codemirrors 사용
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.css">
<link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<script src="https://codemirror.net/lib/codemirror.js"></script>
<script src="https://codemirror.net/addon/edit/matchbrackets.js"></script>
<script src="https://codemirror.net/mode/htmlmixed/htmlmixed.js"></script>
<script src="https://codemirror.net/mode/xml/xml.js"></script>
<script src="https://codemirror.net/mode/javascript/javascript.js"></script>
<script src="https://codemirror.net/mode/css/css.js"></script>
<script src="https://codemirror.net/mode/clike/clike.js"></script>
<script src="https://codemirror.net/mode/php/php.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="codeCtrl">
HTML:<br>
<textarea ui-codemirror ng-model="html"></textarea>
<br>CSS:<br>
<textarea ui-codemirror ng-model="css"></textarea>
</div>
Output:
<section id="output">
<iframe></iframe>
</section>
</div>
</body>
</html>
자바 스크립트 :
는var myApp = angular.module('myApp', ['ui']);
myApp.value('ui.config', {
codemirror: {
mode: 'text/x-php',
lineNumbers: true,
matchBrackets: true,
}
});
function codeCtrl($scope, codeService) {
$scope.html = '<body>default</body>';
$scope.css = "body {color: red}";
$scope.$watch('html', function() { codeService.render($scope.html, $scope.css); }, true);
$scope.$watch('css', function() { codeService.render($scope.html, $scope.css); }, true);
}
myApp.service('codeService', function() {
this.render = function (html, css) {
source = "<html><head><style>" + css + "</style></head>" + html +"</html>";
var iframe = document.querySelector('#output iframe'),
iframe_doc = iframe.contentDocument;
iframe_doc.open();
iframe_doc.write(source);
iframe_doc.close();
}
})
위의 코드는 작동하지만 문제는 하나의 동일한 ui.config
2 ui-codemirror
에 적용됩니다. 누구든지 모드 을 첫 번째 ui-codemirror
에 적용하고 모드 css
을 두 번째 ui-codemirror
에 적용하는 방법을 알고 있습니까?
또한 높이 (또는 rows
)와 너비 (또는 cols
)를 ui-codemirror
으로 설정할 수 있습니까?
감사합니다. 여기 [JSBin] (https://jsbin.com/pelaruhexu/2/edit?html,js,output) –