User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper SweetAlert With AngularJS

Today we will discuss that how we can use the SweetAlert message in AngularJS. There are some pre-request files need to use this functionality.

We can downlaod & install thses file with multiple way :

1. Download these file

2. Install through bower: $ bower install sweetalert

3. Install through NPM: $ npm install sweetalert

Step to use SweetAlert with AngularJS:-

1. Create wrapper JS file for AngularJs:-

angular.module('firstCrazyDeveloper.ngSweetAlert', [])
.factory('SweetAlert', [ '$rootScope', function ( $rootScope ) {

var swal = window.swal;

//public methods
var self = {

swal: function ( arg1, arg2, arg3 ) {
$rootScope.$evalAsync(function(){
if( typeof(arg2) === 'function' ) {
swal( arg1, function(isConfirm){
$rootScope.$evalAsync( function(){
arg2(isConfirm);
});
}, arg3 );
} else {
swal( arg1, arg2, arg3 );
}
});
},
success: function(title, message) {
$rootScope.$evalAsync(function(){
swal( title, message, 'success' );
});
},
error: function(title, message) {
$rootScope.$evalAsync(function(){
swal( title, message, 'error' );
});
},
warning: function(title, message) {
$rootScope.$evalAsync(function(){
swal( title, message, 'warning' );
});
},
info: function(title, message) { 
$rootScope.$evalAsync(function(){
swal( title, message, 'info' );
});
}
};

return self;
}]);

2. Initialize the plugin by referencing the necessary files:

<script src="dist/sweetalert.min.js"></script>
 <link rel="stylesheet" type="text/css" href="dist/sweetalert.css">


3. Examples to use that Alerts:

A.) A basic message -  

 SweetAlert.swal("Good Morning Friends");> 

B.) A title with a text under -

SweetAlert.swal("Here's a message!", "It's pretty, isn't it?");


C.) A success message! -

SweetAlert.swal("Good job!", "You clicked the button!", "success");


D.) A warning message, with a function attached to the "Confirm"-button

SweetAlert.swal({ 
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false},
function(){
SweetAlert.swal("Booyah!");
});


E.) And by passing a parameter, you can execute something else for "Cancel".

SweetAlert.swal({ 
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false },
function(isConfirm){
if (isConfirm) {
SweetAlert.swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
SweetAlert.swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});


F.) A message with a custom icon

SweetAlert.swal({ 
title: "Sweet!",
text: "Here's a custom image.",
imageUrl: "http:://oitozero.com/avatar/avatar.jpg" });

Live Deomo

Configuration

Here are the keys that you can use if you pass an object into sweetAlert:

ArgumentDefault valueDescription
title null (required) The title of the modal. It can either be added to the object under the key "title" or passed as the first parameter of the function.
text null A description for the modal. It can either be added to the object under the key "text" or passed as the second parameter of the function.
type null The type of the modal. SweetAlert comes with 4 built-in types which will show a corresponding icon animation: "warning", "error", "success" and "info". You can also set it as "input" to get a prompt modal. It can either be put in the object under the key "type" or passed as the third parameter of the function.
allowEscapeKey true If set to true, the user can dismiss the modal by pressing the Escape key.
customClass null A custom CSS class for the modal. It can be added to the object under the key "customClass".
allowOutsideClick false If set to true, the user can dismiss the modal by clicking outside it.
showCancelButton false If set to true, a "Cancel"-button will be shown, which the user can click on to dismiss the modal.
showConfirmButton true If set to false, the "OK/Confirm"-button will be hidden. Make sure you set a timer or set allowOutsideClick to true when using this, in order not to annoy the user.
confirmButtonText "OK" Use this to change the text on the "Confirm"-button. If showCancelButton is set as true, the confirm button will automatically show "Confirm" instead of "OK".
confirmButtonColor "#AEDEF4" Use this to change the background color of the "Confirm"-button (must be a HEX value).
cancelButtonText "Cancel" Use this to change the text on the "Cancel"-button.
closeOnConfirm true Set to false if you want the modal to stay open even if the user presses the "Confirm"-button. This is especially useful if the function attached to the "Confirm"-button is another SweetAlert.
closeOnCancel true Same as closeOnConfirm, but for the cancel button.
imageUrl null Add a customized icon for the modal. Should contain a string with the path to the image.
imageSize "80x80" If imageUrl is set, you can specify imageSize to describes how big you want the icon to be in px. Pass in a string with two values separated by an "x". The first value is the width, the second is the height.
timer null Auto close timer of the modal. Set in ms (milliseconds).
html false If set to true, will not escape title and text parameters. (Set to false if you're worried about XSS attacks.)
animation true If set to false, the modal's animation will be disabled.
inputType "text" Change the type of the input field when using type: "input" (this can be useful if you want users to type in their password for example).
inputPlaceholder null When using the input-type, you can specify a placeholder to help the user.
inputValue null Specify a default text value that you want your input to show when using type: "input"

Methods

SweetAlert also comes with some simple methods that you can call:

FunctionExampleDescription
setDefaults swal.setDefaults({ confirmButtonColor: '#0000' }); If you end up using a lot of the same settings when calling SweetAlert, you can use setDefaults at the start of your program to set them once and for all!
close swal.close(); Close the currently open SweetAlert programatically.
showInputError swal.showInputError("Invalid email!"); Show an error message after validating the input field, if the user's data is bad

Note:- SweetAlert was created by Tristan Edwards




crazydeveloper Home Page 03 July 2015

Become a Fan