Reputation: 1024
In Angular, $location.search()
returns an object, which is handy to modify: add new params, alter or remove (set to null
) existing ones. And $location.search(object)
sets this object to the search
component of $location
. I'm looking for a way to get the composed query string from this object leaving the $location
intact. I don't need to get the actual query string, I need to compose the query string from an object.
I don't want to reinvent the wheel and write a javascript function that transforms an object to the string of &-separated key-value pairs, Angular already has one. Basically what I'd like to do is to use toKeyValue
method from Angular.js, which is used to compose query string in the $$compose
function in location.js. However, it seems like toKeyValue
method is inaccessible from outside Angular (unlike, for example, forEach
), as I'm getting angular.toKeyValue is not a function
error when trying to call it. Is there any way to call toKeyValue
method from my controller or just compose the query string from an object by means of Angular?
Upvotes: 2
Views: 603
Reputation: 1317
You can use $httpParamSerializer
that converts objects to strings.
This will do from your controller.
app.controller('MainCtrl', function($scope, $httpParamSerializer) {
var querystring = $httpParamSerializer({ width:1680, height:1050 }); //height=1050&width=1680
});
Upvotes: 3