Skip to content

Instantly share code, notes, and snippets.

@wallacemaxters
Created October 14, 2016 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wallacemaxters/f865ad2d53f93b4389679bec04ba0dd8 to your computer and use it in GitHub Desktop.
Save wallacemaxters/f865ad2d53f93b4389679bec04ba0dd8 to your computer and use it in GitHub Desktop.
confirm and alert dialog using uib-modal of the ui angular bootstrap
angular.module('ui.bootstrap.dialogs', ['ui.bootstrap'])
.factory('$dialogConfirm', function ($uibModal) {
return function (message, title) {
var modal = $uibModal.open({
size: 'sm',
template: '<div class="modal-header">\
<h4 class="modal-title" ng-bind="title"></h4>\
</div>\
<div class="modal-body" ng-bind="message"></div>\
<div class="modal-footer">\
<button class="btn btn-default" ng-click="modal.dismiss()">não</button>\
<button class="btn btn-primary" ng-click="modal.close()">sim</button>\
</div>',
controller: function ($scope, $uibModalInstance) {
$scope.modal = $uibModalInstance;
if (angular.isObject(message)) {
angular.extend($scope, message);
} else {
$scope.message = message;
$scope.title = angular.isUndefined(title) ? 'Mensagem' : title;
}
}
});
return modal.result;
}
})
.factory('$dialogAlert', function ($uibModal) {
return function (message, title) {
var modal = $uibModal.open({
size: 'sm',
template: '<div class="modal-header">\
<h4 class="modal-title" ng-bind="title"></h4></div>\
<div class="modal-body" ng-bind="message"></div>\
<div class="modal-footer">\
<button class="btn btn-primary" ng-click="modal.close()">OK</button>\
</div>',
controller: function ($scope, $uibModalInstance) {
$scope.modal = $uibModalInstance;
if (angular.isObject(message)) {
angular.extend($scope, message);
} else {
$scope.message = message;
$scope.title = angular.isUndefined(title) ? 'Mensagem' : title;
}
}
});
return modal.result;
}
})
@wallacemaxters
Copy link
Author

wallacemaxters commented Oct 14, 2016

Usage example

angular.module('app', ['ui.bootstrap.dialogs', 'ui.bootstrap'])

.controller('AppController', function ($scope, $dialogConfirm) {

    $scope.callAction = function () {

        $dialogConfirm("Deseja continuar?").then(function () {

            // Action if "yes" is pressed

        }, function () {

            // action if "no" is pressed
        })
    }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment