-1

I'm trying to get a parameter from the URL without having a router in the specific page. I've read that you can use $location.search().clearCart and that'll give the value of clearCart . I get undefined. Is there a way of getting this without using a router?

The url is: http://ec2-50-17-53-129.compute-1.amazonaws.com/home?clearCart=true

this is my controller:

    app.controller('cartCtrl', function ($scope, $rootScope, $http, $location, dialogs, ngCart) {
        if(($location.search()).clearCart != 'undefined' && ($location.search()).clearCart == 'true'){
            for(var i = 0; i < ngCart.getTotalItems(); i++){

                ngCart.removeItem(i);
            }
        }
}
7
  • Can you put an example of your url and the output of $location.search() on your question ? Commented Feb 23, 2017 at 20:01
  • $location.search() returns an object which is a series of key:value pairs. Have you tried logging $location.search() to the console to see if your key is in the object? Commented Feb 23, 2017 at 20:01
  • Do you want the value of the Query string parameter or the name of the parameter? Commented Feb 23, 2017 at 20:06
  • I tried to log $location.search() and it doesn't have "clearCart" in it. I also tried ($location.search()).clearCart and got undefined Commented Feb 23, 2017 at 21:00
  • @Dylan - I want the value. Commented Feb 23, 2017 at 21:14

2 Answers 2

0

This isn't really an angular thing, but you can use it in your controller fine.

  var queryStringValue = GetQueryStringParams('clearCart');

    function GetQueryStringParams(sParam) {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) {
                return sParameterName[1];
            }
        }
    };
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you have not injected $location dependecy correctly

You need to inject the dependency $location in order to use it.

Here is a post that solves your problem:

Angular JS: $location injection undefined

In it you can find this plunkr:

app.run(function($rootScope, $location) {
  $rootScope.$on('$routeChangeSuccess', function () {
    if (true) 
      $location.url("/login")
  })
})

http://plnkr.co/edit/gf5LhTjqhz4MoZfVcqIt?p=preview

Hope it helps :)

Comments

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.