我想创建一个简单的markdown指令,该指令接受元素中的某些内容,对其进行解析并将其替换为html。
所以这:
<markdown>#Heading</markdown>
或这个($ scope.heading ='#Heading';)
<markdown>{{heading}}</markdown>
变成这个:
<h1>Heading</h1>
到目前为止,我的指令(显然没有完成!):
.directive('markdown', function () {
return {
restrict: 'E',
replace: true,
link: function ($scope, $element, $attrs) {
// Grab contents
var contents = /* How do I do this? */
var newContents = Markdowner.transform(contents);
// Replace <markdown> element with newContents
/* How do I do this? */
}
}
})
我不确定如何获取指令的内容?我需要编译它吗?
解析Markdown只是一个例子
干得好!
app.directive('markdown', function() {
return {
restrict: 'E',
transclude: true,
compile: function(elem) {
elem.replaceWith(Markdowner.transform(elem.html()));
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句