You can use the sticky state of ui-router-extras to solve your problem. There is a simple example with a modal link. You must create two named views: one for the main content (background) and one for the modal.
<div ui-view="app"></div> <div ui-view="modal"></div>
Mark the state from which you want to access the modal as sticky: true in the route definition.
.state('main', { abstract: true, url: '/', templateUrl: '_layout.html' }) .state('main.index', { url: '', sticky: true, views: { 'app': { templateUrl: 'index.html' } } }) .state('main.login', { url: 'login/', views: { 'modal': { templateUrl: 'login.html' } } })
Also add an event for stateChangeSuccess:
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) { if ((from.views && !from.views.modal) || !from.views) { $rootScope.from = from; $rootScope.fromParams = fromParams; } });
so when you need to close the modal, you can just
$state.go($rootScope.from, $rootScope.fromParams);
There is a small problem for this solution. If you reload the page in a modal state, the ui-view application will be empty.
source share