nosdalg
nosdalg

Reputation: 571

Angular How to capture a particular url in $routeProvider

My URL to capture is

http://test.sample.com/?code=4/CAKREYsOD89sg7eXahK93Pw7pnMb5ndkiCrzSUlMS1U

I need to capture the above url in my $routeProvider. My code is.

$routeProvider.when('/:code/',{
                    template: '',
                    controller: function ($location,$rootScope) {
                        console.log("I got you. .. ");
                    }
                })
                .otherwise({
                    redirectTo: '/login'
                });

My problem is I cant able to capture this url in when.

Any help is appreciated.

Upvotes: 4

Views: 127

Answers (3)

FlavienBert
FlavienBert

Reputation: 1684

There are two ways to do want you need:

  1. If you want to capture a special param, don't put it in a query parameter. Use a url like this :

    http://test.sample.com/#/code/CAKREYsOD89sg7eXahK93Pw7pnMb5ndkiCrzSUlMS1U

And catch it like this :

$routeProvider.when('/code/codeID',{
                template: '',
                controller: function ($routeParams) {
                    console.log($routeParams.codeID);
                }
            })
            .otherwise({
                redirectTo: '/login'
            });
  1. If you want use query parameter you can do it like this:

    $routeProvider.when('/',{
            template: '',
            controller: function ($location) {
               var queryParams = $location.search();
               console.log(queryParams.code);                   
            }
        })
        .otherwise({
            redirectTo: '/login'
        });
    

And use this URL :

http://test.sample.com/?code=4/CAKREYsOD89sg7eXahK93Pw7pnMb5ndkiCrzSUlMS1U

I hope this code will help you!

Upvotes: 0

Nagesh Sanika
Nagesh Sanika

Reputation: 1090

use $routeParam

$routeProvider.when('/:code/',{
                template: '',
                controller: function ($location,$rootScope) {
                   var urlCopied=$location.path(); // get the current path                     
                }
            })
            .otherwise({
                redirectTo: '/login'
            });

hope it helps

Upvotes: 1

Babajide Fowotade
Babajide Fowotade

Reputation: 460

You could try this

// given url http://example.com/#/some/path?foo=bar&baz=xoxo var absUrl = $location.absUrl(); // => "http://example.com/#/some/path?foo=bar&baz=xoxo"

Gotten from Angular js $location

Upvotes: 0

Related Questions