Bootstrap alert message represented as modal, angular - angularjs

Bootstrap 3 provides Bootstrap: event messages: success, info, warning, danger.
However sometimes the view doesn't have enough space to show up the event message.
Is there easy way to wrap event with modal in Angular?
This is a template I started to play with

I'll answer on my own question.
Simple way
The flow is pretty simple and straightforward. We don't reinvent the wheel here.
We don't need nor header neither footer:
Dialog template HTML:
<div class="modal-body" style="padding:0px">
<div class="alert alert-{{data.mode}}" style="margin-bottom:0px">
<button type="button" class="close" data-ng-click="close()" >
<span class="glyphicon glyphicon-remove-circle"></span>
</button>
<strong>{{data.boldTextTitle}}</strong> {{data.textAlert}}
</div>
</div>
We even don't need to use ng-class:
class="alert-{{data.mode}}"
where mode might be: success, info, warning, danger
Modal Instance Controller:
var ModalInstanceCtrl = function ($scope, $modalInstance, data) {
$scope.data = data;
$scope.close = function(/*result*/){
$modalInstance.close($scope.data);
};
};
And this is modal configuration and content:
$scope.data = {
boldTextTitle: "Done",
textAlert : "Some content",
mode : 'info'
}
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
backdrop: true,
keyboard: true,
backdropClick: true,
size: 'lg',
resolve: {
data: function () {
return $scope.data;
}
}
});
Demo Plunker
Directive way
Demo 2 Plunker
We can put all above written code into directive for better maintenance:
HTML
<button class="btn btn-success" ng-click="open()" >success
<my-alert
bold-text-title="Done"
text-alert="Some content"
mode="success"
></my-alert>
</button>
Directive
.directive('myAlert', function($modal,$log) {
return {
restrict: 'E',
scope: {
mode: '#',
boldTextTitle: '#',
textAlert : '#'
},
link: function(scope, elm, attrs) {
scope.data= {
mode:scope.mode,
boldTextTitle:scope.boldTextTitle,
textAlert:scope.textAlert
}
var ModalInstanceCtrl = function ($scope, $modalInstance, data) {
console.log(data);
scope.data= {
mode:scope.mode || 'info',
boldTextTitle:scope.boldTextTitle || 'title',
textAlert:scope.textAlert || 'text'
}
};
elm.parent().bind("click", function(e){
scope.open();
});
scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
backdrop: true,
keyboard: true,
backdropClick: true,
size: 'lg',
resolve: {
data: function () {
return scope.data;
}
}
});
modalInstance.result.then(function (selectedItem) {
scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
}
};
})
Hope it will save time to someone.

I've made a service and controller which depends of eachother:
.service('AlertService', function($uibModal){
/*
headerText - presents text in header
bodyText - presents text in body
buttonText - presents text in button. On its click if method parameters is not passed, modal will be closed.
In situation that the method parameters is passed, on its click, method will be called. For situations
like that, there is parameter buttonText2 which will be used as cancel modal functionality.
method - presents passed function which will be called on confirmation
buttonText2 - presents text in button for cancel
*/
var alert = function(headerText, bodyText, buttonText, method, buttonText2){
method = method || function(){};
buttonText2 = buttonText2 || '';
$uibModal.open({
animation: true,
templateUrl: '/static/angular_templates/alert-modal.html',
controller: 'AlertModalInstanceCtrl',
size: 'md',
resolve: {
headerText: function () {
return headerText;
},
bodyText: function () {
return bodyText;
},
buttonText: function () {
return buttonText;
},
method: function () {
return method;
},
buttonText2: function () {
return buttonText2;
}
}
});
};
return{
alert: alert
};
})
.controller('AlertModalInstanceCtrl', function ($scope, $uibModalInstance, headerText, bodyText, buttonText, method, buttonText2) {
$scope.headerText = headerText;
$scope.bodyText = bodyText;
$scope.buttonText = buttonText;
$scope.method = method;
$scope.buttonText2 = buttonText2;
$scope.ok = function () {
$scope.method();
$uibModalInstance.dismiss('cancel');
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
and html file:
<!--Modal used for alerts in AlertService-->
<div class="modal-header">
<h3 class="modal-title">{[{ headerText }]}</h3>
</div>
<div class="modal-body">
<p>{[{ bodyText }]}</p>
</div>
<div class="modal-footer">
<button class="btn btn-default" ng-click="cancel()" ng-if="buttonText2">{[{ buttonText2 }]}</button>
<button class="btn btn-primary" ng-click="ok()">{[{ buttonText }]}</button>
</div>
Now, depending for what type you want to use it, you have a few options:
-If you pass headerText, bodyText and buttonText, it will behave like a classic alert modal
AlertService.alert('Some header', 'Some message', 'Text button');
-If you pass headerText, bodyText, buttonText and method, it will behave like a classic alert modal but with the function which you can pass and later handle in the controller
AlertService.alert('Are you sure?', 'Are you sure you want to create this round', 'Ok', $scope.createRound);
$scope.createRound = function(){
//do something
}
-And the last one. If you pass all the parameters, it will act like the previous one, just with the possibility to cancel and close modal.
AlertService.alert('Are you sure?', 'Are you sure you want to create this round', 'Ok', $scope.createRound, 'Cancel');
$scope.createRound = function(){
//do something
}
Of course, if you want to use this, you'll have to inject angular ui bootstrap. I wasted a lot of time to develop this, but it worth. It was annoying to create every time a new controller, new template and all the other things.
From the controller then you can easily use it, just inject it first.

Thanks for answering your own question, it was helpful.
Here's a version as a service you can wire in and fire off from any controller without needing to include directive mark-up.
It uses the latest angular UI Bootstrap paradigm for modals.
It has some convenience methods (info, error, warn, success).
It fires off an event when closed with the data as an event argument in case you need to know that.
Enjoy!
angular.module('modal.alert.service', [], function ($provide) {
'use strict';
$provide.factory('ModalAlertService', ['$rootScope', '$uibModal',
function ($rootScope, $uibModal) {
var factory = {
alert: function(mode, title, text) {
var modalData = {
mode : mode,
title : title,
text : text
};
var modalInstance = $uibModal.open({
template: '<div class="modal-body" style="padding:0px">' +
'<div class="alert alert-{{data.mode}}" style="margin-bottom:0px">' +
'<button type="button" class="close" data-ng-click="close()" >' +
'<span class="glyphicon glyphicon-remove-circle"></span>' +
'</button><strong>{{data.title}}</strong>: {{data.text}}</div></div>',
controller : 'ModalAlertController',
backdrop : true,
keyboard : true,
backdropClick : true,
size : 'lg',
resolve : {
data : function() {
return modalData;
}
}
});
modalInstance.result.then(function(data) {
$rootScope.$broadcast('modal-alert-closed', { 'data' : data });
});
},
info: function(title, text) {
factory.alert('info', title, text);
},
error: function(title, text) {
factory.alert('danger', title, text);
},
warn: function(title, text) {
factory.alert('warning', title, text);
},
success: function(title, text) {
factory.alert('success', title, text);
}
};
return factory;
}]);
}).controller('ModalAlertController', function ($scope, $uibModalInstance, data) {
$scope.data = data;
$scope.close = function() {
$uibModalInstance.close($scope.data);
};
});

Related

angular binding to a popover

I are trying to bind a $scope.model.value to a popover.
With all the articles I have something working by storing a copy in the $scope then using a watch to update it if it changes who ever that is not a viable solution as I want to take the popover and use it multiple times with different arrays.
my html on the the main body is:
<button href="#" colorpopover
type="button"
class="btn btn-success btn-rainbow"
data-toggle="popover"
data-trigger="click"
title="Button Color"
id="static-color-popover-{{$id}}"
ng-model="model.value.buttonStatic.buttonColor">
Button Color
</button>
my controller has this code
app.directive('colorpopover', function ($compile, $templateCache, $q, $http) {
var getTemplate = function () {
var def = $q.defer();
var template = '';
template = $templateCache.get('/App_Plugins/IBD.ButtonStyles/Popovers/IBD.Color.Popover.html');
if (typeof template === "undefined") {
$http.get("/App_Plugins/IBD.ButtonStyles/Popovers/IBD.Color.Popover.html").then(function (data) {
$templateCache.put("templateId.html", data);
def.resolve(data);
});
} else {
def.resolve(template);
}
return def.promise;
}
return {
restrict: "A",
link: function (scope, element, attrs, model) {
getTemplate().then(function (popOverContent) {
// Make sure to remove any popover before hand (please confirm the method)
var compileContent = $compile(popOverContent.data)(scope);
var options = {
bindToController: true,
content: compileContent,
placement: "left",
html: true,
date: scope.date, };
$(element).popover(options);
});
}
};
});app.directive('colorpopover', function ($compile, $templateCache, $q, $http) {
var getTemplate = function () {
var def = $q.defer();
var template = '';
template = $templateCache.get('/App_Plugins/IBD.ButtonStyles/Popovers/IBD.Color.Popover.html');
if (typeof template === "undefined") {
$http.get("/App_Plugins/IBD.ButtonStyles/Popovers/IBD.Color.Popover.html").then(function (data) {
$templateCache.put("templateId.html", data);
def.resolve(data);
});
} else {
def.resolve(template);
}
return def.promise;
}
return {
restrict: "A",
link: function (scope, element, attrs, model) {
getTemplate().then(function (popOverContent) {
// Make sure to remove any popover before hand (please confirm the method)
var compileContent = $compile(popOverContent.data)(scope);
var options = {
bindToController: true,
content: compileContent,
placement: "left",
html: true,
date: scope.date, };
$(element).popover(options);
});
}
};
});
the template in basic terms
is
<div class="row">
<div ng-repeat="colorGroup in model.value" ng-class="!$last ? '' : 'last'">
<p>{{colorGroup.val1}}</p>
<p>{{colorGroup.val2}}</p>
<p>{{colorGroup.val3}}</p>
the model structure is
$scope.model.value.buttonStatic.buttonColor[val1,val2,val3]
$scope.model.value.buttonStatic.buttonHover[val1,val2,val3]
$scope.model.value.buttonStatic.buttonFocus[val1,val2,val3]
so eventually I want three buttons as a above with each of the tree values passed.
so at present the ng-repeat is repeating on the model.value of the child scope which is a direct copy of the parent.
the value in the template is going to change on the popover so it needs to go back to the parent.
With much trial and error I have solved the problem.
in the directive I needed to tie add these 2 line before the link.
require: 'ngModel', // ensures the model is passed in
scope: { model: '=ngModel' }, //ties the ng-model to the scope of the popover
then all I needed to do was set the ng-model in my button to model.value......
and in the popover template i just use model

How to make the two way binding in AngularJS Isolated scope?

directive('confButton', function () {
return {
restrict: 'EA',
replace: false,
scope: {
modalbtntext: '#',
btntext: '#',
iconclass: '#',
btnclass:'#',
callback: '&',
disabled: '='
},
templateUrl : '/app/scripts/mod/Directives/ConfirmationDirective/ConfrimationDirect.html',
controller: ['$scope', '$element', '$attrs', '$transclude', 'modalService',
function ($scope, $element, $attsr, $transclude, modalService) {
$scope.open = function () {
console.log($scope.disabled);
var bodyMessage = '';
if ($scope.modalbtntext.toLowerCase() == "edit") {
bodyMessage = "Are you sure you want to edit this ?"
}
else{
bodyMessage = 'Are you sure you want to delete this customer?'
}
var modalOptions = {
closeButtonText: 'Cancel',
actionButtonText: $scope.modalbtntext,
headerText: 'Please Confirm your Request',
bodyText: bodyMessage
};
modalService.showModal({}, modalOptions).then(function (result) {
$scope.callback();
});
}
}]
}
});
and this is my Tempalte
<button class="{{btnclass}}" ng-disabled="{{disabled}}" ng-click="open()"><i class="{{iconclass}}"></i> {{btntext}} </button>
here is the implementation of the directive
<conf-button modalbtntext="Delete"
disabled="gridOptions.selectedItems.length == 0"
btntext="Delete Selected"
btnclass="btn btn-default hidden-xs"
iconclass ="glyphicon glyphicon-trash"
callback="deleteCity()">
</conf-button>
The point is I want to implement the two way binding in the button .. it's disabled by default as no item is selected .. but when I choose any item it still disabled. how can I achieve the two way binding in this case ?
To make 2 way binding to work you need to bind it to an object. What happens is that you bind the value to a primitive during the expression evaluation so the binding doesn't update.
You can use $watch to update a certain value.
.controller('someCtrl', function($scope){
$scope.gridOption.selectedItems = [];
$scope.$watch(function(){
//Watch for these changes
return $scope.gridOptions.selectedItems.length == 0;
}, function(newVal){
//Change will trigger this callback function with the new value
$scope.btnDisabled = newVal;
});
});
HTML markup:
<conf-button modalbtntext="Delete"
disabled="btnDisabled"
btntext="Delete Selected"
btnclass="btn btn-default hidden-xs"
iconclass ="glyphicon glyphicon-trash"
callback="deleteCity()">
</conf-button>

Attributes in custom modal directive not resolved

I created a custom directive to display a ui.bootstrap modal and I cannot populate the dialog with the values I want. I don't understand why is it not working. I checked other questions and posts but I could not find and example suitable for my case.
I've created a plunker with a similar version of my real code to show the problem.
In the example, I am using a button to launch the modal but in my application I am doing it by code when some conditions are satisfied.
This is the link http://plnkr.co/edit/7CCV7uHHj7SrWbsp1PRJ
Basically I have a directive:
var app = angular.module('app', ['ui.bootstrap']);
app.directive('revModal', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
id: "#",
title: "#",
size: "#",
bodyMessage: '#',
acceptButton: '#',
dismissButton: '#'
},
controller: function($scope, $modal) {
$scope.title = $scope.title ? $scope.title : "Alert";
$scope.size = $scope.size ? $scope.size : "sm";
$scope.acceptButton = $scope.acceptButton ? $scope.acceptButton : "OK";
$scope.dismissButton = $scope.dismissButton ? $scope.dismissButton : "Cancel";
$scope.openModal = function() {
var modalInstance = $modal.open({
templateUrl: 'modalAlert.html',
animation: true,
backdrop: true,
windowClass: 'app-modal-window',
size: $scope.size,
resolve: {
acceptButton: $scope.acceptButton,
dismissButton: $scope.dismissButton
}
});
modalInstance.result.then(function() {}, function() {
console.log('Modal dismissed at: ' + new Date());
});
};
},
link: function($scope, element) {
element[0].open = function() {
return $scope.openModal();
}
}
}
});
In the directive I've also tried to resolve the values using function but it didn't work either.
resolve:{
dismissButton: function(){return $scope.dismissButton}
...
}
The directive template is the following, and my problem is that the values inside curly braces are not replaced. The logic to link the ng-click functions was not done yet.
<div class="modal-header">
<h3 class="modal-title">{{title}}</h3>
</div>
<div class="modal-body">
{{bodyMessage}}
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="acceptModal()">{{acceptButton}}</button>
<button class="btn btn-warning" ng-click="dismissModal()">{{dismissButton}}</button>
</div>
To use this directive I just insert the following tag in the html:
<div ng-controller="TimeRangeModalController">
<rev-modal id="timeRangeModal" title="test" accept-button="acepto" dismiss-button="calcelo" body-message="cuerpo"></rev-modal>
</div>
The controller to open the modal is:
var app = angular.module('app');
app.controller('TimeRangeModalController', function($scope, $document) {
$scope.open= function(modalId){
if (undefined !== modalId) {
var modal = $document[0].getElementById(modalId);
if (null !== modal) {
modal.open();
} else {
console.error('DOM element with id ' + 'modalId' + 'does not exist');
}
}
}
});
Any suggestion on how should I resolve the values to populate the modal?
Thanks and regards, Daniela.
Finally I've discovered the issue, I was missing to set the scope in the modalInstance.
The final open modal function will be:
$scope.openModal = function() {
var modalInstance = $modal.open({
templateUrl: 'modalAlert.html',
animation: true,
backdrop: true,
windowClass: 'app-modal-window',
size: $scope.size,
scope:$scope
});
Note also that the resolve clause is not need it.
I've updated the Plunkr http://plnkr.co/edit/7CCV7uHHj7SrWbsp1PRJ

Not able to pass scope to dialog in angular

I am displaying a list of images using Angular.
When an image is clicked I want to display a dialog with the image and more info.
So when opening the modal I need to pass the scope, or at least, the image.
Note: I am using ngDialog for the popup.
application.controller('ImageController', function ImageController($scope, ImageService, ngDialog) {
$scope.model = {
images: [],
loading: false
}
var load = function () {
$scope.model.loading = true;
ImageService.GetList($scope.model.pages.instagram)
.success(function (data, status, headers, config) {
$scope.model.images = $scope.model.images.concat(data.Images)
})
.error(function (data, status, headers, config) { })
.finally(function () {
$scope.model.loading = false
});;
}
$scope.open = function (image) {
ngDialog.open({
template: '<p>my template {{image.Key}} </p>',
plain: true,
scope: $scope
});
};
load();
});
<div data-ng-app="Application" data-ng-controller="ImageController">
<div data-ng-repeat='image in model.images'>
<img src="{{image.Url}}" alt="" data-ng-click="open(image)"/>
</div>
The images are displayed and the dialog is opened on click ...
However, somehow, I am not able to access the scope inside the template.
The following works:
$scope.open = function (image) {
ngDialog.open({
template: '<p>my template' + image.Key + '</p>',
plain: true
});
};
But this does not work:
$scope.open = function (image) {
ngDialog.open({
template: '<p>my template {{image.Key}} </p>',
plain: true,
scope: $scope
});
};
Does anyone has any idea why?
I haven't been able to figure this out.
change your open method to following..
$scope.open = function (image) {
var newScope = $scope.$new();
newScope.image = image;
ngDialog.open({
template: '<p>my template {{image.Key}} </p>',
plain: true,
scope: newScope
});
};
This should work ...
The best way that works for me is without scope. just use data attribute:
$scope.open = function (image) {
ngDialog.openConfirm({
template: 'views/alertTemplate.html'
showClose: false,
data: {
image: image,
}
});
}
and in your template file:
<div class="alert-dialog-container">
<div class="alert-dialog-body">
<p ng-bind-html="ngDialogData.image"></p>
</div>
<div class="alert-dialog-footer">
<button ng-click="confirm()">OK</button>
</div>
</div>

angularjs custom confirm box

So I'm trying to implement a custom confirm box using Angular. Ideally, I would simply like to add an attribute to enable the functionality. Example:
<button type="button" ng-click="delete(foo)">Delete</button> -> <button type="button" ng-click="delete(foo)" ng-confirm="Are you sure you want to delete this foo?">Delete</button>
(foo is inside an ng-repeat... foo in fooList..)
So all of the problems I am having revolve around tying the click event that would normally happen to a different button. I have a seperate directive "confirmBox" that will create my modal (not using bootstrap) and handle all of the showing/hiding/etc.
What I am currently using requires me to alter my ng-click functionality, which I really want to get away from:
Current Implementation:
<button ... ng-click="confirm('Are you sure you want to delete this foo?, 'delete', foo)">Delete</button>
var confirmModule = angular.module('confirm', []);
confirmModule.run(function($rootScope) {
$rootScope.confirm = function(text, func, obj) {
$rootScope.$broadcast('confirm', func, obj, text);
};
});
confirmModule.directive('confirmBox', function($parse) {
return {
restrict: 'A',
template: myModalTemplate,
link: function(scope, element, attrs){
element.hide();
var noBtn = element.find("[name='no']");
noBtn.bind("click", function() {
element.hide();
});
scope.$on("confirm", function(event, func, obj, text) {
var yesBtn = element.find("[name='yes']");
element.show();
yesBtn.unbind("click").bind("click", function() {
scope[func](obj);
});
});
}
}
});
Anyone have any ideas? I started by adding the directive for the button and then unbinding the click event so ng-click doesn't fire. Then I am left with the string 'delete(foo)' from the ng-click attribute that I can execute with $parse(attrs.ngClick)(scope), but I don't know how to tie that to the separate directives button click.
Edit: Here is a fiddle with my current attempt at implementation. The problem is the variable being passed in to the function is always undefined.
http://jsfiddle.net/UCtbj/2/
Edit2: Updated implementation, however I don't particularly like how it links the two directives together by targetting the other directives elements.
http://jsfiddle.net/UCtbj/3/
It seems to me that you're trying to do things the jQuery way from within the directive. However, what you want is as simple as pulling in the UI-Bootstrap directive for confirming actions. http://plnkr.co/edit/JhfAF1?p=preview
First simple service for modal windows:
app.service('ConfirmService', function($modal) {
var service = {};
service.open = function (text, onOk) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalConfirmCtrl',
resolve: {
text: function () {
return text;
}
}
});
modalInstance.result.then(function (selectedItem) {
onOk();
}, function () {
});
};
return service;
})
app.controller('ModalConfirmCtrl', function ($scope, $modalInstance, text) {
$scope.text = text;
$scope.ok = function () {
$modalInstance.close(true);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
Then simple directive that uses it:
app.directive('confirm', function(ConfirmService) {
return {
restrict: 'A',
scope: {
eventHandler: '&ngClick'
},
link: function(scope, element, attrs){
element.unbind("click");
element.bind("click", function(e) {
ConfirmService.open(attrs.confirm, scope.eventHandler);
});
}
}
});
And here u go:
<button ng-click="test(12)" confirm='Are you sure?'>Button</button>
http://plnkr.co/edit/LOZOnsVyx3JU5XoKYn74?p=preview
To allow a button to be marked up like
<button type="button" ng-click="deleteItem(drink)" ng-confirm="Are you sure you want to delete '{{drink.name}}'">Delete</button>
You can write a directive that
Intercepts the click event before ngClick's click handler can run
Opens a dialog (using $modal and not the removed $dialog)
On close of the dialog (which is treated as a success) run the function specified by the ngClick attribute on the element.
Basing the code on the previous answer, you can do this as follows:
app.directive('ngConfirm', function($modal, $parse) {
return {
// So the link function is run before ngClick's, which has priority 0
priority: -1,
link: function(scope, element, attrs) {
element.on('click', function(e) {
// Don't run ngClick's handler
e.stopImmediatePropagation();
$modal.open({
templateUrl: 'ng-confirm-template',
controller: 'ngConfirmController',
resolve: {
message: function() {
return attrs.ngConfirm;
}
}
}).result.then(function() {
// Pass original click as '$event', just like ngClick
$parse(attrs.ngClick)(scope, {$event: e});
});
});
}
};
});
which needs a simple controller:
app.controller('ngConfirmController', function($scope, $modalInstance, message) {
$scope.message = message;
$scope.yes = function() {
$modalInstance.close();
};
$scope.no = function() {
$modalInstance.dismiss();
};
});
and template for the dialog:
<script type="text/ng-template" id="ng-confirm-template">
<div class="modal-body">
<p>{{message}}</p>
</div>
<div class="modal-footer">
<button class="btn btn-link pull-left" ng-click="no()">No</button>
<button class="btn btn-primary pull-right" ng-click="yes()">Yes</button>
</div>
</script>
You can see this running at http://plnkr.co/edit/Gm9lFsGb099w6kCMQoVY?p=preview
Edit: changed plunker link to example without scrollbar appearing/disappearing on display of the dialog
Here is a nice directive for that.That is ngBootbox. Check it out.
<button class="btn btn-lg btn-primary"
ng-bootbox-title="A cool title!"
ng-bootbox-custom-dialog="Some custom text"
ng-bootbox-buttons="customDialogButtons"
ng-bootbox-class-name="some-class">
Custom dialog
</button>
<script>
$scope.customDialogButtons = {
warning: {
label: "Warning!",
className: "btn-warning",
callback: function() { $scope.addAction('Warning', false); }
},
success: {
label: "Success!",
className: "btn-success",
callback: function() { $scope.addAction('Success!', true) }
},
danger: {
label: "Danger!",
className: "btn-danger",
callback: function() { $scope.addAction('Danger!', false) }
},
main: {
label: "Click ME!",
className: "btn-primary",
callback: function() { $scope.addAction('Main...!', true) }
}
};
</script>
Demo
ngBootbox
I created a repo for this functionality. It wraps the ui-bootstrap modal to produce a confirmation box. It is customizable and easily integrated into any application.
Here is the link to the GitHub: https://github.com/Schlogen/angular-confirm
Example Usages:
As a directive:
<button type="button" ng-click="delete()" confirm-if="checked" confirm="Are you sure, {{name}}?">Delete</button>
As a service:
$confirm({text: 'Are you sure you want to delete?'})
.then(function() {
$scope.deletedConfirm = 'Deleted';
});
Ok, here is the one I ended up going with
1) Create a service for the dialog
app.service('dialogModal', [
'$modal', function($modal) {
return function(message, title, okButton, cancelButton) {
okButton = okButton === false ? false : (okButton || 'Yes');
cancelButton = cancelButton === false ? false : (cancelButton || 'No');
var modalInstance = $modal.open({
templateUrl: '/templates/deletePrompt.html',
controller: ModalInstanceCtrl,
resolve: {
settings: function() {
return {
modalTitle: title,
modalBody: message,
okButton: okButton,
cancelButton: cancelButton
};
}
}
});
// return the modal instance
return modalInstance;
}
}
]);
2) Create a controller and pass the model instance in it
var ModalInstanceCtrl = function ($scope, $modalInstance, settings) {
angular.extend($scope, settings);
$scope.ok = function () {
$modalInstance.close(true);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
3) included the link into the header to take on default styling
<link data-require="bootstrap-css#3.x" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
4) overwrote the styling in my own css
5) Here is my delete prompt template
<div id="overlayClearMainDiv" class="dialog-modal">
<div id="overlayClearText">
<span>{{modalBody}}</span>
</div>
<div id="overlayClearButton">
<button id="overlayClearYesButton" class="confirmButton" type="button" ng-click="ok()" ng-show="okButton">{{okButton}}</button>
<button class="confirmButton-white" ng-click="cancel()" ng-show="cancelButton">{{cancelButton}}</button>
</div>
</div>
Here's a quick one for you - http://plnkr.co/edit/YklthDZcknmvMjU5A6pe?p=preview
So basically if you are interested in showing a modal dialog once a user clicks on let's say, a button there's no need to make it difficult.
All you need is a simple directive that encapsulate $modal service found in ui-bootstrap.
In my simple example I just pass in a string representing a message and then defining a on-confirm attribute that my directive invokes once the user confirms. Invoking the function itself leverages the awesomeness of $parse to resolve the expression and once resolved, invoke it with the scope.
Nice and clear and here's how it looks like.
View
<body ng-controller="AppController">
<input type="button" value="Delete"
confirm="'Are you sure you want to delete me?'" on-confirm="delete()" />
<script type="text/ng-template" id="modal.html">
<div class="modal-header">
<h3 class="modal-title">Confirm</h3>
</div>
<div class="modal-body">
<p>The world won't be a better place if you delete me.</p>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
</body>
Controller / Directive
angular
.module('App', ['ui.bootstrap'])
.controller('AppController', ['$scope', function($scope){
$scope.delete = function(){
alert('Woho, Im deleted!');
};
}])
.directive('confirm', ['$modal', '$parse', function($modal, $parse){
return {
link: function(scope, el, attr){
el.bind('click', function(){
var instance = $modal.open({
templateUrl: 'modal.html',
controller: ['$scope', '$modalInstance', modalController]
});
instance.result.then(function(){
// close - action!
$parse(attr.onConfirm)(scope);
},function(){
// dimisss - do nothing
});
});
}
};
function modalController(modalScope, $modalInstance){
modalScope.ok = function(){
modalInstance.close();
};
modalScope.cancel = function(){
modalInstance.dismiss();
};
}
}]);

Resources