使用angularjs和bootstrap中。如果url变化,modal框不会主动消失,因此,使用directive来控制。
.directive('discardModal', ['$rootScope', '$modalStack',
function($rootScope, $modalStack) {
return {
restrict: 'A',
link: function() {
/**
* If you are using ui-router, use $stateChangeStart method otherwise use $locationChangeStart
* StateChangeStart will trigger as soon as the user clicks browser back button or keyboard backspace and modal will be removed from modal stack
*/
$rootScope.$on('$locationChangeStart', function (event) {
var top = $modalStack.getTop();
if (top) {
$modalStack.dismiss(top.key);
}
});
}
};
}
]);
html代码
<div class="add-config-modal" discard-modal>
//modal内容
</div>