javascript AngularJS 过滤器以字符串形式返回 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17561149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:46:56  来源:igfitidea点击:

AngularJS filter returning html as string

javascriptangularjsangularjs-filter

提问by Jakemmarsh

I've created an AngularJS filter to automatically create clickable links from addresses found in data. The filter:

我创建了一个 AngularJS 过滤器来自动从数据中找到的地址创建可点击的链接。过滤器:

app.filter('parseUrl', function() {
    var  //URLs starting with http://, https://, or ftp://
        replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
        //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
        replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
        //Change email addresses to mailto:: links.
        replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;

        return function(text, target, otherProp) {        
            angular.forEach(text.match(replacePattern1), function(url) {
                text = text.replace(replacePattern1, "<a href=\"\" target=\"_blank\"></a>");
            });
            angular.forEach(text.match(replacePattern2), function(url) {
                text = text.replace(replacePattern2, "<a href=\"http://\" target=\"_blank\"></a>");
            });
            angular.forEach(text.match(replacePattern3), function(url) {
                text = text.replace(replacePattern3, "<a href=\"mailto:\"></a>");
            });

            return text;        
        };
    });

And here is how I'm calling it (inside a paragraph):

这是我如何称呼它的(在一个段落中):

<p><strong>Details:</strong> {{event.description | parseUrl}}</p>

And this works correctly to replace the plain text links with the code for a link. However, it replaces it with the link literally as plain text. For example, www.google.comwould get replaced with &lt;a href="http://www.google.com" target="_blank"&gt;http://google.com&lt;/a&gt;. This clearly doesn't make a clickable link, which was my goal.

这可以正确地用链接代码替换纯文本链接。但是,它用纯文本形式的链接替换了它。例如,www.google.com将被替换为&lt;a href="http://www.google.com" target="_blank"&gt;http://google.com&lt;/a&gt;. 这显然不会产生可点击的链接,这是我的目标。

I'm not sure why this is happening. Any ideas on how to prevent/fix it? Thanks.

我不确定为什么会这样。关于如何预防/修复它的任何想法?谢谢。

采纳答案by Jollymorphic

Try using the ngBindHtmlUnsafedirective to have the HTML that your filter produces applied as actual innerHTML contents of an element, like so:

尝试使用ngBindHtmlUnsafe指令将过滤器生成的 HTML 应用为元素的实际 innerHTML 内容,如下所示:

<span ng-bind-html-unsafe="event.description | parseUrl"></span>

回答by Clark Pan

You need to use either:

您需要使用:

Outputting a string using an expression will escape any html entities that you pass to it (symbols such as < > &)

使用表达式输出字符串将转义您传递给它的任何 html 实体(诸如 < > & 之类的符号)

回答by djvs

I was using this filter for a while, somehow without noticing the disastrous results it produced. My modified version here:

我用了一段时间这个过滤器,不知何故没有注意到它产生的灾难性结果。我的修改版本在这里:

filter('parseUrl', function($sce) {
var  //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
    //Change email addresses to mailto:: links.
    replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;

    return function(text, target, otherProp) {        
        text = (text + '').replace(/>/,"&gt;").replace(/</,"&lt;");
        text = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '<br>');
        text = text.replace(replacePattern1, "<a href=\"\" target=\"_blank\"></a>");
        text = text.replace(replacePattern2, "<a href=\"http://\" target=\"_blank\"></a>");
        text = text.replace(replacePattern3, "<a href=\"mailto:\"></a>");
        return $sce.trustAsHtml(text);
    };
});

Notice that it's not using angular.forEach! (?????) The output would go ballistic when it did. Presumably this problem has to do with having multiple matches!

请注意,它没有使用 angular.forEach!(???) 输出会在它发生时弹道。据推测,这个问题与有多场比赛有关!