2

I have a route to handle creation, modification, view and list of users like this :

angular
  .module('abcApp', ['ui.router'])
  .config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state('user.add', {
        url: '/mgt/user/add',
        templateUrl: 'views/mgt_user/add.html',
        controller: 'MgtUserCtrl'
      })
      .state('user.view', {
        url: '/mgt/user/view/:userId',
        templateUrl: 'views/mgt_user/view.html',
        controller: 'MgtUserCtrl'
      })
      .state('user.list', {
        url: '/mgt/user/list',
        templateUrl: 'views/mgt_user/list.html',
        controller: 'MgtUserCtrl'
      });
  });

I want to use one route where I can pass the mode as param and get the right templateUrl:

angular
  .module('abcApp', ['ui.router'])
  .config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state('userMangement', {
        url: '/mgt/user/:mode/:userId',
        templateUrl: 'views/mgt_user/' + $routeParams.mode + '.html',
        controller: 'MgtUserCtrl'
      });
  });

mode can be : view, edit, list.

Is there a way to do this?

1 Answer 1

4

You can use templateProvider.

$stateProvider
    .state('userManagement', {
        url: '/mgt/user/:mode/:userId',
        controller: 'MgtUserCtrl',
        templateProvider: ['$route', '$templateCache', '$http', function($route, $templateCache, $http) {
            var url = '/views/mgt_user/' + $route.current.params.mode + '.html';
            $http.get(url, {cache: $templateCache}).then(function(html) {
                return html;
            });
        }]
 })

You cannot use $routeparams here becuase $routeParams is available after $routeChangeSuccess event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.