Can not get $routeParams - AngularJS -

May 07, 2015

Reading time ~1 minute

Outline

You can not get $routeparams.key in $routeProvider resolve section.

Example

angular.module('app')
    .config(Router);

  Router.$inject = ['$routeProvider'];

  function Router($routeProvider) {
    $routeProvider
      .when('/path/:iuserId',
          {
            templateUrl: '/templates/path.html',
            controller: 'PathController',
            resolve: {
              user: ['$routeParams', 'PathResource', function($routeParams, PathResource) {
                var id = $routeParams.userId; // you can not get parameter from $routeProvider
                return PathResource.get(id);.
              }]
            }
          });

Reason

In $routeScope.$locationChangeSuccess event, execute resolve section to get data before Rouing then $routeParams are updated.

Solution

angular.module('app')
    .config(Router);

  Router.$inject = ['$routeProvider'];

  function Router($routeProvider) {
    $routeProvider
      .when('/path/:iuserId',
          {
            templateUrl: '/templates/path.html',
            controller: 'PathController',
            resolve: {
              user: ['$route', 'PathResource', function($route, PathResource) {
                var id = $route.current.params.id; // you can get parameter from $route.current
                return PathResource.get(id);.
              }]
            }
          });

$route.current is updated in $routeScope.$locationChangeStart event which is fired before execute resolve section ($routeScope.$locationChangeSuccess)

add ticket number to git commit automatically

Most of ticket tracker like Github, pivotal tracker have function to connect your commit to ticket(story).But every time when you commit ...… Continue reading

I will build frontend and backend separately

Published on September 28, 2016

order of angular controller's initialisation

Published on October 05, 2015