0

I have a URL like that: http://myproject.com/apartments?location=123&arrival=01&departure=456&rooms=789

How can I get the value of location, arrival, departure, rooms and pass it to my service.

I try to using $location.search() but I can not get the values. The result: Object { location=undefined, arrival=undefined, departure=undefined, more...}

My serivce:

angular.module('apartmentService', [])

    .factory('Apartment', function ($http) {
        return {
            get: function (parameters) {

                console.log(parameters);

                return $http({
                    method: 'GET',
                    url: '/api/apartments',
                    params: {location: parameters}
                });
            }
        };
    });

My controller:

angular.module('apartmentCtrl', [])

    .controller('ApartmentController', function ($scope, $http, $location, Apartment) {
        $scope.loading = true;

        $scope.parameters = {
            location: $location.search().location,
            arrival: $location.search().arrival,
            departure: $location.search().departure,
            rooms: $location.search().rooms
        };

        Apartment.get($scope.parameters).success(function (data) {
            $scope.apartments = data;
            $scope.loading = false;
        });
    });
8
  • your URL is a string. So, using string.split(), string.indexOf() should be enough, no? Commented Feb 25, 2015 at 14:55
  • I'm newbie. You can give me a demo? Thank you very much !!! Commented Feb 25, 2015 at 14:57
  • 1
    There are plenty of samples on the net : developer.mozilla.org/fr/docs/Web/JavaScript/Reference/… Commented Feb 25, 2015 at 14:59
  • What is the angular version? Commented Feb 25, 2015 at 15:03
  • I don't understand, cai you give demo with angularjs? Commented Feb 25, 2015 at 15:03

5 Answers 5

3

Ugly unsecure two-liner:

paramsObject = {};
window.location.search.replace(/\?/,'').split('&').map(function(o){ paramsObject[o.split('=')[0]]= o.split('=')[1]});

paramsObject will be:

Object {location: "123", arrival: "01", departure: "456", rooms: "789"} 

which you can inject directly into your $scope.parameters

plunker here

Sign up to request clarification or add additional context in comments.

Comments

1

You're mapping them incorrectly, to retrieve the queryStrings you do:

$location.search('arrival')
$location.search('location') 
// etc etc

But you don't really need to map them... $location.search() returns an object (key/value) of all the parameters

$scope.parameters = $location.search();

// would return an object { location : '123', arrival : '555' /* etc etc */ }

4 Comments

Thank you. But when I edit to: $scope.parameters = $location.search();. The empty object is returned.
Do you definitely have things at the end of your URL? whatever.html?location=123&arrival=555 It only returns empty object when there is nothing in the querystring
If my url like: http://sleepy.dev:8000/apartments?location=123&arrival=01&departure=456&rooms=789. It return empty object. But when I add the # to my url: http://sleepy.dev:8000/apartments#?location=123&arrival=01&departure=456&rooms=789. It will be OK. But when I submit the form through GET method, the # is not exits.
I'm not sure what you mean, but the # and ? are both required in order for it to see the params!
1

Use ui-router. You will love it and it's the proper approach! Trust me!

There is a great example from this link - https://github.com/angular-ui/ui-router/wiki/URL-Routing

Comments

0

$location.search('queryparamname');

inside the controller inject the service $location

see the reference

https://docs.angularjs.org/api/ng/service/$location

// given url http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}

// set foo to 'yipee'
$location.search('foo', 'yipee');
// $location.search() => {foo: 'yipee', baz: 'xoxo'}

3 Comments

use like key value see the documentation
// given url example.com/#/some/path?foo=bar&baz=xoxo var searchObject = $location.search(); // => {foo: 'bar', baz: 'xoxo'} // set foo to 'yipee' $location.search('foo', 'yipee'); // $location.search() => {foo: 'yipee', baz: 'xoxo'}
It return the empty object :(
0

I am guessing it is because of angular is expecting the data after the url hash.

Meaning that $location.search should work if the url look like that: http://myproject.com/#apartments?location=123&arrival=01&departure=456&rooms=789.

One thing you can try, is to let the app know its base. Add the following line to your html head tag.

<base href="/">

1 Comment

Joshua, the # and ? are both required for it to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.