Skip to content

Instantly share code, notes, and snippets.

@pranav7
Last active February 8, 2016 07:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranav7/d075f7cd8263159cf36a to your computer and use it in GitHub Desktop.
Save pranav7/d075f7cd8263159cf36a to your computer and use it in GitHub Desktop.
SweetAlert + Angular - EASY

Sweetalert + Angular - SUPER EASY 👍

SweetAlert

There are a few simple libraries to make Angular work with Sweetalert. If you really want to check them out:

Unfortunately, they wern't that "Sweet". The idea is to not use libraries to use other libraries.

Here is a simple module that anyone can write around Sweetalert, to make things work.

# sweetalert.coffee

do ->
  'use strict'

  SweetAlert = ->
    return window.swal

  angular
    .module('sweetalert', [])
    .factory('swal', SweetAlert)

Simply include sweetalert in your app.js or app.coffee.

# app.coffee

do ->
  'use strict'
  
  angular.module('yourApp', ['sweetalert'])

Usage

Once setup, you can simply call sweetalert by calling swal.

do ->
  'use strict'
  
  DemoController = ($scope) ->
    $scope.btnClickHandler = ->
      swal('Hello, World!')
  
  angular
    .module('demo', ['sweetalert'])
    .controller('DemoController', DemoController)

You just saved yourself from adding an extra library to run another library. :beers:

@goldcaddy77
Copy link

Nice, I've used a similar method to access underscore.

Note - Since you didn't dependency inject swal into 'DemoController', you're using the reference on window..

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