I am using UI-Router and angular bootstrap-ui. I have a state setting for creating a modal "onEnter". I'm having problems right now when I try to close the modal "onExit". Here is the main condition. It will open modal mode when "item.detail" is entered, and it will go to "elements" when this modal is closed or rejected.
.state('items.detail', { url: '/{id}', onEnter: function ($stateParams, $state, $modal, $resource) { var modalInstance = $modal.open({ templateUrl: 'views/modal/item-detail.html', controller: 'itemDetailCtrl' }) .result.then(function () { $state.transitionTo('items'); }, function () { $state.transitionTo('items'); }); } })
I tried using the onExit handler. But they could not access the modalInstance or the area in which the modal is located inside this handler. Everything that I try to enter appears undefined.
.state('items.detail', { url: '/{id}', onEnter: function ($stateParams, $state, $modal, $resource) { var modalInstance = $modal.open({ templateUrl: 'views/modal/item-detail.html', controller: 'itemDetailCtrl' }) .result.then(function () { $state.transitionTo('items'); }, function () { $state.transitionTo('items'); }); }, onExit: function ($scope) { controller: function ($scope, $modalInstance, $modal) { $modalInstance.dismiss(); }; } })
from my modal controller, I tried listening for state changes.
$scope.$on('$stateChangeStart', function() { $modalInstance.dismiss(); });
I tried this as with $ scope. $ on and $ rootScope. $ on and both of these work, but in the end they are called every time I switch between any states. This happens only after I opened the modal.
In case this last bit is unclear ... When I update my angular application, I can switch between all my other states without calling this listener event, but after I open this modal, all my state changes will be called by this listener , even after the modal is closed.