jac0117 jac0117 - 8 days ago 6
AngularJS Question

AngularJS - Retrieve param values from the route (URL)

I have the following URL:

http://mysite:9000/#/sso/salesforce/666


I added my route the following way:

$routeProvider
.when('/sso/salesforce/:companyId', { ....


I'm trying to resolve the companyId from the URL, in this case
666
:
http://mysite:9000/#/sso/salesforce/666

I'm injecting $location into a resolve function to resolve the company ID, but I'm not sure how to get it from $location.

$location.search()
gives me an object with a property 'companyId: true'
I don't need to know that is present, I need to get the actually value
666
.

How is this done in angular 1? I'm new to angular, not sure how.

Answer

You can inject and use $routeParams for this as below.

In the config,

$routeProvider.when('/sso/salesforce/:companyId', {
            templateUrl: '/sso/salesforce/' + $routeParams.companyId + '.html'
        });

In the controller,

function RouteController($scope, $routeParams) {
        $scope.templateUrl = '/sso/salesforce/' + $routeParams.companyId + '.html';
    }
Comments