I am trying to make a directive for HTML using AngularJS so that I can render Markdown in the browser. What I want is to have a <markdown>
tag with a src
attribute that will load the file specified and render it correctly.
I have partially implemented this as follows -
function Main($scope) {
$scope.theContent = '#asgakfgajgfas\n##akfaljfqpo\ndhvkajvlbndvm';
};
angular.module('myApp', [])
.directive("markdown", function ($compile) {
return {
restrict: 'E',
require: 'model',
scope: {
value: "=model"
},
template: '<div ng-bind-html-unsafe="value | markdown"></div>'
};
}).filter('markdown', function () {
var converter = new Showdown.converter();
return function (value) {
return converter.makeHtml(value || '');
};
});
And the corresponding HTML -
<div ng-controller="Main">
<markdown model="theContent"></markdown>
</div>
Here is the jsFiddle link(based on John Linquist's example) for the above code. This does not work with the src
attribute, however it is able to load a markdown text string specified in a model.
Could you tell me how I can change this code to load the file specified in the src
tag. I was thinking of using the $http
provided by AngularJS but couldn't get my head around actually using it inside the directive definition.
What I would like to achieve is <markdown src="a/b/c.md" />