Alex M.
Alex M.

Reputation: 641

Best way to get all URL parameters in angularjs

I got this URL into my system:

http://localhost/myapp/#/campaign/confirm/edit/:id

What are the best ways to get campaign, confirm and editinside my controller?

Upvotes: 2

Views: 696

Answers (1)

HankScorpio
HankScorpio

Reputation: 3651

Looks like you've got this url formatted as an angular state (awesome!). In this case, you can access $stateParams in your controller.

https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

That should have the information you need, assuming your state is something like this. This example makes all 4 parts into accessible variables, that's if you really do need ALL parts of that path. It's probably worth putting in some non-variable data in there, otherwise it might get a little difficult to separate this state from others that are formed in a similar way:

$stateProvider.state('myStateName', {
  url: '{campaign}/{confirm}/{edit}/{id}'
});

myModule.controller('fooCtrl', function($scope, $stateParams){
  console.log(
    $stateParams.campaign,
    $stateParams.confirm,
    $stateParams.edit,
    $stateParams.id);
});

Upvotes: 1

Related Questions