Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add sweet alert angular js

I'm new to AngularJS and I'm trying to use sweet alert plugin from https://github.com/oitozero/ngSweetAlert , I already added the corresponding scripts to my index.html like this :

index.html

<link rel="stylesheet" href="bower_components/sweetalert/dist/sweetalert.css">
<script src="bower_components/angular-sweetalert/SweetAlert.min.js"></script>
<script src="bower_components/sweetalert/dist/sweetalert.min.js"></script>

As the documentacion says. Now in my ctrl.js I have this :

var ctrl = function ($scope, SweetAlert) {

    SweetAlert.swal("Here's a message"); // this does not work
}

ctrl.$inject = ['$scope', 'oitozero.ngSweetAlert];

angular.module('myApp.missolicitudes.controllers',
    [
       'oitozero.ngSweetAlert'
    ])
    .controller('MiSolicitudCtrl', ctrl);

But is not working, on my browser I got this error from developer tools:

Error: [$injector:unpr] http://errors.angularjs.org/1.4.3/$injector/unpr?p0=oitozero.ngSweetAlertProvider%20%3C-""itozero.ngSweetAlert%20%3C-%20MiSolicitudCtrl at Error (native) at http://localhost:8081/assets/libs/angular/angular.min.js:6:416 at http://localhost:8081/assets/libs/angular/angular.min.js:40:375 at Object.d [as get] (http://localhost:8081/assets/libs/angular/angular.min.js:38:364) at http://localhost:8081/assets/libs/angular/angular.min.js:40:449 at d (http://localhost:8081/assets/libs/angular/angular.min.js:38:364) at e (http://localhost:8081/assets/libs/angular/angular.min.js:39:124) at Object.instantiate (http://localhost:8081/assets/libs/angular/angular.min.js:39:273) at $get (http://localhost:8081/assets/libs/angular/angular.min.js:80:228) at link (http://localhost:8081/assets/libs/angular/angular-route.min.js:7:268)

How can I implement this plugin correctly?

Update 1

I have already try this suggestion by @Pankaj Parkar and @Mike G

ctrl.$inject = ['$scope', 'oitozero.ngSweetAlert'];

and like this

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

My developer tools throws this message:

Error: [$injector:unpr] http://errors.angularjs.org/1.4.3/$injector/unpr?p0=oitozero.ngSweetAlertProvider%20%3C-"<div class="container ng-scope" ng-view="">"itozero.ngSweetAlert%20%3C-%20MiSolicitudCtrl
at Error (native)
at http://localhost:8081/assets/libs/angular/angular.min.js:6:416
at http://localhost:8081/assets/libs/angular/angular.min.js:40:375
at Object.d [as get] (http://localhost:8081/assets/libs/angular/angular.min.js:38:364)
at http://localhost:8081/assets/libs/angular/angular.min.js:40:449
at d (http://localhost:8081/assets/libs/angular/angular.min.js:38:364)
at e (http://localhost:8081/assets/libs/angular/angular.min.js:39:124)
at Object.instantiate (http://localhost:8081/assets/libs/angular/angular.min.js:39:273)
at $get (http://localhost:8081/assets/libs/angular/angular.min.js:80:228)
at link (http://localhost:8081/assets/libs/angular/angular-route.min.js:7:268)
like image 707
Luis Felipe Garcia Avatar asked Aug 19 '15 19:08

Luis Felipe Garcia


People also ask

What is alert in Angularjs?

Alert is a part of 'window' (an object of a Browser), which is a global variable. In this blog entry we will discuss how to show alert in angularjs. Alert is a part of 'window' (an object of a Browser), which is a global variable. In Javascript we can use simply. alert('Javascript Alert!


1 Answers

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

like image 53
Pranav Singh Avatar answered Oct 03 '22 07:10

Pranav Singh