-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Export buildUrl in the global angular namespace #3213
Conversation
buildUrl could be used outside of $http too, for example if the user needs to create urls for APIs that are not natively supported in angular (EventSource for ex.) potentionally if it's not hidden in a closure inside $http, it might be used in other parts of angular too
There is a related discussion in #2701 I still claim that this URL params parsing / formatting functionality should go into its own service. |
I agree there's probably a bigger story about url handling. On the other hand this is a simple change that really doesn't change much. Maybe it's a good interim step? |
@gdamjan The problem with this interim step is that it adds a method to the public API. We would have to maintain "forever". |
I agree. |
I don't have much to add to #2701 and don't know about a more generic way to go. |
@gdamjan It is not only about one function - serialization and deserialization go hand in hand. This is why I believe that we need a service that would encapsulate those 2 functions. |
@gdamjan FYI I have a use for this but I don't use the |
I would love it if i can properly build a url for my JSONP request without having to resort to hacks. We need a solution for this, but I understand it's not just $location that's touched, it's a lot of changes deep into $http as well. For people landing here from search, this is my current solution: /**
* Angular's private URL Builder method + unpublished dependencies converted to a public service
* So we can properly build a GET url with parameters for a JSONP request.
*/
app.provider('URLBuilder', function() {
function encodeUriQuery(val, pctEncodeSpaces) {
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
}
/**
* Angular's private buildUrl function, patched to refer to the public methods on the angular globals
*/
function buildUrl(url, params) {
if (!params) return url;
var parts = [];
angular.forEach(params, function(value, key) {
if (value === null || angular.isUndefined(value)) return;
if (!angular.isArray(value)) value = [value];
angular.forEach(value, function(v) {
if (angular.isObject(v)) {
v = angular.toJson(v);
}
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));
});
});
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
}
this.$get = function() {
return {
build: function(url, params) {
return buildUrl(url, params);
}
};
};
}); ``` |
+1 |
Is there a solution for this in angular 1.3 ? |
I'm also waiting for this one, has it been included in latest angular yet? |
As far as I know (from looking at the source etc), this is not in 1.3 but there may be a solution coming in 1.4, for now I am manually copying this changeset in to my copy of angular |
👍 |
Docs for 1.4+ found here. |
buildUrl could be used outside of $http too, for example if the user needs
to create urls for APIs that are not natively supported in angular (EventSource for ex.)
potentionally if it's not hidden in a closure inside $http,
it might be used in other parts of angular too