everyone. Sometimes I need some way to get some part of external site url in my angular code. But there's obviously no way to do it with native angular services, and I don't wanna use some Jquery library for parsing url. I need some service for url parsing which. Where can I find such service? Thx!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can do it in Vanilla JavaScript.
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
Source: https://gist.github.com/jlong/2428561
回答2:
I guess you could use the $sce service:
function($scope, $sce) {
$scope.getHtml = function() {
return $sce.trustAsResourceUrl(myUrlHere);
};
}
As mentioned in other post, provided that this is not subject to CORS.
回答3:
By Parsing, I'm assuming you mean fetching of content/data from a url.
Proving the url allows any domain ( 'Access-Control-Allow-Origin', '*' ) or your domain, Angular does have ajax functions built in, simular to jquery et all.
http://docs.angularjs.org/api/ng.$http
Copied direct from the anguar doc
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});