i have one issue.my requirement is when user will try to move to next state/page one confirm box will appear and if user is clicking on ok button then only it will navigate to next state otherwise it will remain as it is.I am explaining my code below.
<li ui-sref-active="active" ><a ui-sref=".profile" confirm="Are you sure to move into next page?">Profile</a></li>
<li ui-sref-active="active"><a ui-sref=".dept" >Department</a></li>
Suppose user clicked on profile menu,first one confirm box will come and after pressing ok it will move.
dashboard.directive('confirm', function(ConfirmService, $state) {
return {
restrict: 'A',
scope: {
eventHandler: '&ngClick'
},
link: function(scope, element, attrs){
element.unbind("click");
element.bind("click", function(e) {
e.preventDefault(); // block the href generated by ui-sref
ConfirmService.open(attrs.confirm, $state.go(attrs.uiSref)); // pass ui-sref into $state.go
});
}
}
});
dashboard.service('ConfirmService', function($uibModal) {
var service = {};
service.open = function (text, onOk) {
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalConfirmCtrl',
resolve: {
text: function () {
return text;
}
}
});
modalInstance.result.then(function (selectedItem) {
onOk();
}, function () {
});
};
return service;
})
dashboard.controller('ModalConfirmCtrl', function ($scope, $uibModalInstance, text) {
$scope.text = text;
$scope.ok = function () {
$uibModalInstance.close(true);
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
updated js code
dashboard.directive('confirm', function($uibModal, $state,$timeout) {
return {
restrict: 'A',
scope: {
eventHandler: '&ngClick'
},
link: function(scope, element, attrs){
var isConfirmed = false;
element.bind("click", function (e) {
if (!isConfirmed) {
e.preventDefault();
$uibModal.open({
templateUrl: 'myModalContent.html',
controller: function ($scope, $uibModalInstance) {
//$scope.text=text;
$scope.ok = function () { $uibModalInstance.close(); }
$scope.cancel = function () { $uibModalInstance.dismiss(); }
}
})
.result.then(function () {
isConfirmed = true;
$timeout(function () {
element.click();
});
});
}
});
}
}
});
Here also the confirm box is coming but problem is it is first moving to profile state and showing the confirm box.I need the before moving to the next state the confirm box should display and after pressing ok it will navigate.i tried using ng-click there but by using this it is missing the active class and cursor:pointer property on the menu.Please help me to resolve this issue.
ui-srefto next view onOK buttonand close modal on click onCancel button. That should do..I guess..ui-srefattribute toOKbutton to go to other state(state that you want to go on click ofokbutton) and on 'CANCEL' button simply close the modal.