我按照 Alp 的建议做了一个指令。如果其他人遇到这个问题,这是我的解决方案:
首先是降价指令:
.directive('markdown', ['$sanitize',function ($sanitize) {
var converter = new Showdown.converter();
return {
restrict: 'AE',
link: function (scope, element, attrs) {
if (attrs.markdown) {
scope.$watch(attrs.markdown, function (newVal) {
var html = newVal ? $sanitize(converter.makeHtml(newVal)) : '';
element.html(html);
});
} else {
var html = $sanitize(converter.makeHtml(element.text()));
element.html(html);
}
}
};
}])
transdown指令转换密钥,然后在输出上使用 markdown 指令。
.directive('transdown', ['$translate', function ($translate) {
'use strict';
return {
restrict: 'AE',
replace: true,
scope: {
key: '@'
},
template: "<span markdown='translation'></span>",
link: function(scope, element, attrs){
scope.$watch('key', function(n,o){
if( n !== undefined ){
$translate(n).then(function(res){
scope.translation = res;
});
}
});
}
};
}]);