我在我的 angular 应用程序中尝试了 ngSweetAlert。当我尝试使用 SweetAlert.swal() 错误时,我的开发人员工具 TypeError: swal is not a function。
片段。
(function() {
'use strict';
var app = angular.module('iCheckMobile', ['oitozero.ngSweetAlert']);
var BASE_URL = 'http://localhost:8000/';
app.controller('MobileCntrlr', ['$http', '$scope', '$window', '$location', '$interval', '$timeout', 'iCheckMobileFactory', '$filter', 'SweetAlerts', function($http, $scope, $window, $location, $interval, $timeout, iCheckMobileFactory, $filter, SweetAlerts) {
var mbleCntrl = this;
.
.
.
///我使用 ngSweetAlert 的区域
var forceSubmitExam = function() {
// $window.confirm('Submiting exammination automatically');
SweetAlerts.swal('Submitting Examination Automatically', 'You have not enough time', 'success');
var submitInfo = {
'answers': JSON.stringify(mbleCntrl.answers),
'student_id': mbleCntrl.studentDetail.id,
'subject_slug': mbleCntrl.subject_slug,
'subject_id': subject_id
};
$http.post(BASE_URL + 'mobile/examination/submit', submitInfo)
.success(function(response) {
mbleCntrl.answers = '';
console.log(response);
})
.error(function() {
$window.confirm('Error on submitting the examination!');
})
};
SweetAlert.js
angular.module('oitozero.ngSweetAlert', [])
.factory('SweetAlerts', [ '$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;
}]);
我认为它有问题,你们能帮帮我吗,^^ 在 Angular 方面完全是新手。谢谢。
最佳答案
首先包含 Js 文件
<script src="components/sweetalert/dist/sweetalert.min.js"></script>
比包含 Angular 文件
<script src="components/angular-sweetalert/SweetAlert.js"></script>
关于angularjs - angularjs 中使用 ngSweetAlert 的 TypeError : swal is not a function in SweetAlert. js,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34114962/