Reputation: 1368
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?
Upvotes: 2
Views: 4039
Reputation: 1039
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.
Upvotes: 4