如何缩短URL显示域只angular.js过滤器 [英] How to shorten url to show domain only with angular.js filter

查看:80
本文介绍了如何缩短URL显示域只angular.js过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一些的Json长的URL,我想弄清楚采用了棱角分明的过滤器或者只显示领域的最佳途径只是一些JavaScript?

I got some long urls in my Json and I'm trying to figure out the best way to show only the domain using angular's filter or maybe just some javascript ?

的http:// www.example.com/page-with-a-long-long-long-OMG-so-long-name.html

www.example.com

www.example.com

谢谢!

推荐答案

这真的很容易在AngularJS创建自己的过滤器:

It's really easy in AngularJS to create your own filter:

app.filter( 'domain', function () {
  return function ( input ) {
    var matches,
        output = "",
        urls = /\w+:\/\/([\w|\.]+)/;

    matches = urls.exec( input );

    if ( matches !== null ) output = matches[1];

    return output;
  };
});

您可以在您的视图方便地调用:

Which you can easily call in your view:

<span>{{ myUrl | domain }}</span>

下面是一个Plunker:<一href=\"http://plnkr.co/edit/bVSv7n?builder&p=$p$pview\">http://plnkr.co/edit/bVSv7n?builder&p=$p$pview

Here's a Plunker: http://plnkr.co/edit/bVSv7n?builder&p=preview

这是一个超级简单的正则表达式,你可能要扩大,但它的作品!

This is a super-simple regex that you'll probably want to expand, but it works!

这篇关于如何缩短URL显示域只angular.js过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆