How do you access route parameters in AngularJs controller

I have a simple angular example where I cannot get the route parameters for the query string in the url. How to do it?

Controller:

appRoot.controller('RegistrationController', ['$scope', '$resource', '$routeParams', function ($scope, $resource, $routeParams) {

if ($routeParams)
    debugger;


}]);

      

Main config file

var appRoot = angular.module('registrationApp', ['ngRoute', 'registration.directives', 'ngResource']);     //Define the main module + dependancies


//Sets up AngularJS module and routes and any other config objects
appRoot
.config(['$routeProvider', function ($routeProvider) {
    //Setup routes to load partial templates from server. TemplateUrl is the location for the server view (Razor .cshtml view) - Frigin amaze-balls
    $routeProvider
        .when('/registration/:inviteToken', { templateUrl: 'registration', controller: 'RegistrationController' })
        .otherwise({ redirectTo: '/home' });
}])
.controller('RootController', ['$scope', '$route', '$routeParams', '$location', function ($scope, $route, $routeParams, $location) {
    $scope.$on('$routeChangeSuccess', function (e, current, previous) {
        $scope.activeViewPath = $location.path();
    });
}]);

      

When I go through something like http://example.org/registration/?inviteToken=2354234 the route parameters are always empty objects. I also tried http://example.org/registration#/?inviteToken=2354234

It might be worth noting that http://example.org/registration is an MVC controller

Can someone help me understand what I am doing wrong?

+3


source to share


1 answer


In your configuration, you specified a route like this:

http://example.org/#/registration/2354234
(where $routeParams.inviteToken will be 2354234)

      

If you want to match a url like this http://example.org/registration/?inviteToken=2354234

you need a different route:



/registration/?inviteToken=:inviteToken

      


<sub> I don't know about this, but the routes you specified refer to the path after #

if you are not using (and configured correctly on both client and server side). HTML 5 mode. Sub>

0


source







All Articles