如何从 AngularJS 应用程序 View 中的 URL 开头删除“http://”?
我在数据库中有这样的 URL:
http://example.com/
http://example.com
example.com
但我只需要展示
example.com
在 View 中。
最佳答案
这涉及 HTTP 和 HTTPS 或任何其他 URL。它使用内置的 URL 类,它将处理所有您没有正确想到的事情。
app.filter('domain', function () {
return function (input) {
try {
var url = new URL(input);
return url.hostname;
} catch (DOMException) {
// Malformed URL. Return original (or something else).
return input; }
};
});
正确但您可能没有想到的 URL:
- http://example.com
- http://example.com:8000
- http://me@example.com
- file://example.com
- https://example.com
- http://example.com/some-path
- http://example.com?some-query-url
您现在可能不需要它们,但使用正确的库函数意味着您的应用将来不会在有人尝试将其用于其他用途时意外中断。
关于javascript - 如何从 View 中的域名中删除 "http://",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27014636/