I am trying to use the external module, angular-ui-codemirror
, to display the $element.html()
of an enclosing AngularJS directive in a code-formatted block, using nested directive ui-codemirror
.
If you want to know why I need to do this, look here.
I can easily see from examples how to get this going with static text. And I can pass the innerHTML
of the enclosing directive alright. It just doesn't compile afterward into a ui-codemirror
directive.
I see, here, that it is probably necessary to use the $compile
service to do this, but I cannot adapt that example to this situation.
Here is some example AngularJS code:
angular.module('articles', ['ui.codemirror']).directive('jsCode', function($compile) {
return {
restrict: 'E',
link: function($scope, $element) {
$scope.codeText = $element.html();
var template = '<div ' +
'ui-codemirror="{ ' +
'lineNumbers: true, ' +
'theme:\'twilight\', ' +
'readOnly: \'nocursor\', ' +
'lineWrapping: true, ' +
'mode: \'xml\'}" ' +
'ng-bind="codeText"></div>';
var linkFn = $compile(template);
var content = linkFn($scope);
$element.replaceWith(content)
}
};
});
and the html:
<js-code>
<html style="color: green">
<!-- this is a comment -->
<head>
<title>HTML Example</title>
</head>
<body>
The indentation tries to be <em>somewhat &quot;do what
I mean&quot;</em>... but might not match your style.
</body>
</html>
</js-code>
I created this Plunker to illustrate my dilemma. The first block is unformatted. The second block (static) is formatted.
replace in jsCode directive
by
and use
instead of