Since you are dealing with two separate text areas that have different roles (or imagine if they were larger), it makes sense to define separate directives for them, each of which accepts a different configuration object. I created JSBin , which shows one possible approach, through the factory directive, which can be used to create different "mirrors".
angular.module('codeMirrorApp') .factory('CodeMirrorFactory', ['$parse', function($parse) { return { createDirective: function(config) { var configString = JSON.stringify(config); return { scope: true, restrict: 'E', template: '<textarea ui-codemirror=' + configString + ' ng-model="content"></textarea>', controller: ['$scope', '$attrs', function($scope, $attrs) { var handler = $parse($attrs.handler); $scope.$watch('content', function(value) { handler($scope, { content: value }); }); }] }; } }; } ]);
I intentionally use handlers provided by the parent controller instead of bindings to the parent scope, as this makes things more understandable even when viewing HTML markup.
Controller:
angular.module('codeMirrorApp') .controller('MirrorsController', ['RenderMirrors', function(RenderMirrors) { var ctrl = this, html, css; ctrl.handleHtml = function(htmlString) { html = htmlString; RenderMirrors.render(html, css); }; ctrl.handleCss = function(cssString) { css = cssString; RenderMirrors.render(html, css); }; } ]);
Markup:
<div ng-app="codeMirrorApp"> <div ng-controller="MirrorsController as ctrl"> HTML:<br> <html-code-mirror handler="ctrl.handleHtml(content)"></html-code-mirror> <br>CSS:<br> <css-code-mirror handler="ctrl.handleCss(content)"></css-code-mirror> </div> Output: <section id="output"> <iframe></iframe> </section> </div>
Hope this helps.
petkov.np
source share