1. 程式人生 > >url變化的時候,modal框沒有消失

url變化的時候,modal框沒有消失

使用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>