add sweet alert angular js

断了今生、忘了曾经 提交于 2019-12-04 05:45:49

Here is a simple module that I wrote to make SweetAlert work.

 // sweetalert.js

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

function SweetAlert() {
  return window.swal;
};

Hence, no need to use any other library to use sweetalert, simply write your own.

Simply inject the module in the controller where you want to use it.

Example

angular
  .module('demo', ['sweetalert'])
  .controller('DemoController', DemoController);

function DemoController($scope) {
  $scope.btnClickHandler = function() {
    swal('Hello, World!');
  };
};

Here is an example gist in coffeescript: https://gist.github.com/pranav7/d075f7cd8263159cf36a

I got it work, by NOT injecting it in the module.

my ctrl.js just got like this

ctrl.$inject = ['$scope'];

and inside my controller y just call it like this

var ctrl = function ($scope) {
    swal("Here's a message");
}

And it works!, i dont know if the correct way... but at least works.

Inject sweetalert.min.js and sweetalert.css. Used like this code in your controller

swal({
        type: "error",
        title: "Error!",
        text: "fail",
        confirmButtonText: "OK"
      });

`

Never inject a module into to controller as dependency. You should inject SweetAlert factory there which has various function to show alerts. Also add the missing ' qoute on the factory injection.

You should use

ctrl.$inject = ['$scope', 'SweetAlert'];//<==`oitozero.ngSweetAlert` module               
                                            //could be injectable inside your app module.

Demo Plunkr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!