我已經構建了自己的確認消息,以便完全控制它。我的問題是如何在出現此消息時禁用背景(阻止用戶與其交互)。 我不想使用jQuery或UI Bootstrap模塊。當出現自定義確認消息時禁用背景
<body>
<confirmation-message ng-if="showConfirmMessage">Changes have not been saved. Are you sure you wish to leave this page?</confirmation-message>
<script>
angular.module('myApp',[])
.run(['$rootScope', '$state', '$location', function($rootScope, $state, $location) {
'use strict';
$rootScope.showConfirmMessage=false;
var towardsState,towardsParams;
$rootScope.$watch('changeState', function(newVal) {
if (newVal){
$state.go(towardsState, towardsParams);
}
});
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
towardsState=toState;
towardsParams=toParams;
if ($rootScope.dirtyValueForm) {
event.preventDefault();
$rootScope.showConfirmMessage=true;
$rootScope.dirtyValueForm=false;
$rootScope.changeState=false;
}
});
}])
.directive('confirmationMessage', function($rootScope) {
return {
transclude:true,
templateUrl:'app/shared/directives/confirmationMessage.html',
link:function(scope,element){
element.on('click', function(event) {
event.preventDefault();
if ($(event.target).text()==='Okay') {
$rootScope.showConfirmMessage=false;
....
}
else if ($(event.target).text()==='Cancel') {
$rootScope.showConfirmMessage=false;
...
}
});
}
};
});
</script>
</body>
confirmationMessage.html
<div class="confirm-message" >
<ng-transclude></ng-transclude>
<br/>
<br/>
<button>Okay</button>
<button>Cancel</button>
</div>
和CSS
.confirm-message {
position: fixed;
top:220px;
left:50%;
z-index:1000;
width: 400px;
height:150px;
padding:15px;
margin-left: -200px;
text-align: center;
border: 1px solid #000;
background: #293C6E;
color:white;
}
不要徹底檢查的代碼;它確實有效。例如,$rootScope.dirtyValueForm
的值來自另一個控制器。 我希望您在禁用彈出窗口(.confirm-message)時禁用背景而不禁用彈出窗口本身的幫助。
通常它是由具有DIV這需要在屏幕的整個寬度和高度,定位在對話框後面,但在內容上,它註冊了一個點擊監聽器和調用'stopPropagation'在它的前面完成。 – ValLeNain