这是一个巨大的痛苦,但我终于让它工作了。我从 sanitize 模块中取出链接过滤器并将其修改为指令。它基本上通过某种模式的正则表达式将一个范围变成两个,如 OP 中所述。所以我从 scope.post.comment 开始,提取我的模式并将其放置在另一个范围中,并且剩余部分留在评论中。
app.directive('commentHandler', [
function() {
return {
restrict: 'A',
scope: {
post: '=',
thread: '='
},
templateUrl: "pages/comment.html",
controller: function($scope, $sce, $sanitize) {
re = /(>>(\d{1,4})).*/;
raw = $scope.post.comment;
html = [];
quote = [];
while (match = raw.match(re)) {
url = match[2];
i = match.index;
addText(raw.substr(0, i));
addLink(url, match[1]);
raw = raw.substring(i + match[1].length);
}
addText(raw);
$scope.post.comment = raw;
$scope.post.quote = $sanitize(quote.join(''))
function addText(text) {
html.push(text);
}
function addLink(url, text) {
quote.push('<a ');
quote.push('href="');
quote.push('post/' + $scope.thread.id + '/' + url);
quote.push('">');
quote.push(text);
quote.push('</a>');
}
}
};
}
]);