As far as I understand, your goal is to create a directive that allocates the code given as a constant dynamically using a variable or a combination of the two above. If you are not attached to the syntax from your post, here is the solution.
, Prism , {{variables}} . .
, '=' '@' .
:
scope: {
source: '='
}
:
scope: {
source: '@'
}
Angular:
@ @attr - DOM . , DOM . attr , , . : {localName: '@myAttr'}, localName hello {{name}}. name , localName . ( ).
html (, !):
<pre nag-prism source="{{code}}" class="language-css"></pre>
<pre nag-prism source="body {
color: red;
}
{{code}}" class="language-css"></pre>
:
app.directive('nagPrism', [function() {
return {
restrict: 'A',
scope: {
source: '@'
},
link: function(scope, element, attrs) {
scope.$watch('source', function(v) {
if(v) {
Prism.highlightElement(element.find("code")[0]);
}
});
},
template: "<code ng-bind='source'></code>"
};
}]);
:
edit:
, . ( , , ):
app.directive('nagPrism', ['$compile', function($compile) {
return {
restrict: 'A',
transclude: true,
scope: {
source: '@'
},
link: function(scope, element, attrs, controller, transclude) {
scope.$watch('source', function(v) {
element.find("code").html(v);
Prism.highlightElement(element.find("code")[0]);
});
transclude(function(clone) {
if (clone.html() !== undefined) {
element.find("code").html(clone.html());
$compile(element.contents())(scope.$parent);
}
});
},
template: "<code></code>"
};
}]);
, , , :