You can get and set the compiled contents of the element in the link function using:
element.html() //get
element.html("blah") //set
Here is a sample based on Sergiu's sample below that processes the bindings contained within the html using scope.$eval(), before calling the markdown converter:
http://jsfiddle.net/edeustace/4rE85/1/
angular.module('transclude', [])
.directive('markdown', function() {
var regex = /\{\{(.*?)\}\}/;
var converter = new Showdown.converter();
return {
restrict: 'E',
replace: true,
scope: true,
link: function (scope, element) {
var processTemplate = function(text){
var results = text.match(regex);
if(!results){
return text;
} else {
var value = scope.$eval(results[1]);
var replaceKey = new RegExp("{{" + results[1] + "}}","g");
text = text.replace(replaceKey, value);
return processTemplate(text);
}
};
var text = element.text();
var processed = processTemplate(text);
var markdownText = converter.makeHtml(processed);
element.html(markdownText);
}
};
});
which will work with:
<markdown>
# Bar {{foo}} {{foo}}
# {{bing}}
</markdown>
Or you can bind it to an attribute that you can then use in your directive:
app.directive('markdownWithBinding', function () {
var converter = new Showdown.converter();
return {
restrict: 'E',
scope: {
'md' : '@'
},
link: function ($scope, $element, $attrs) {
$scope.$watch('md', function(newMd){
var markdownText = converter.makeHtml(newMd);
element.html(markdownText);
});
}
}
});
Used like so:
<markdown-with-binding md="Hello {{name}}"></markdown-with-binding>
<!-- outputs Hello World!!! -->
Old Answer
This will happen in link() which is for linking the scope to the element. For structural changes where no scope is required you may be better off making your changes in the compile function:
app.directive('markdown', function () {
var link = function ($scope, $element, $attrs) {};
return {
restrict: 'E',
replace: true,
compile: function($element, $attrs, $transclude){
if($element.html() == "#Hello"){
$element.html("<h1>Hello</h1>");
}
return link;
},
}
});
Here's a great tutorial on components: http://www.youtube.com/watch?v=A6wq16Ow5Ec
ng-transclude? (docs.angularjs.org/api/ng.directive:ngTransclude)template: '<h1 ng-transclude></h1>')