-
-
Save CatinhoCR/44aa613af886c36e5ca320193a786742 to your computer and use it in GitHub Desktop.
My version of the AngularJS ui-bootstrap alert service as derived from here: https://coderwall.com/p/r_bvhg/angular-ui-bootstrap-alert-service-for-angular-js. No $rootScopes were harmed in the making of this code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular.module('myApp') | |
.factory('alertService', alertService); | |
function alertService() { | |
var service = { | |
add: add, | |
closeAlert: closeAlert, | |
closeAlertIdx: closeAlertIdx, | |
clear: clear, | |
get: get | |
}, | |
alerts = []; | |
return service; | |
function add(type, msg) { | |
return alerts.push({ | |
type: type, | |
msg: msg, | |
close: function() { | |
return closeAlert(this); | |
} | |
}); | |
} | |
function closeAlert(alert) { | |
return closeAlertIdx(alerts.indexOf(alert)); | |
} | |
function closeAlertIdx(index) { | |
return alerts.splice(index, 1); | |
} | |
function clear(){ | |
alerts = []; | |
} | |
function get() { | |
return alerts; | |
} | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular.module('myApp') | |
.controller('BodyCtrl', BodyCtrl); | |
BodyCtrl.$inject = ['$scope', 'alertService']; | |
function BodyCtrl($scope, alertService) { | |
$scope.alerts = alertService.get(); | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html ng-app="myApp" class="no-js"> | |
<head> | |
<title></title> | |
<link rel="stylesheet" href="styles/main.css"> | |
</head> | |
<body ng-controller="BodyCtrl"> | |
<navigation></navigation> | |
<div class="main container"> | |
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="alert.close()">{{ alert.msg }}</alert> | |
<div ng-view=""></div> | |
</div> | |
<site-footer></site-footer> | |
<script src="angular.js"></script> | |
<script src="ui-bootstrap.js"></script> | |
<script src="scripts/app.js"></script> | |
<script src="scripts/services/alertservice.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular.module('myApp') | |
.controller('SomeCtrl', SomeCtrl); | |
SomeCtrl.$inject = ['$scope', '$http', ' alertService']; | |
function SomeCtrl($scope, $http, alertService) { | |
$http.put('http://some.url/user/44', { | |
firstName: 'John', | |
lastName: 'Doe' | |
}) | |
.then(updateUserSuccess) | |
.catch(updateUserFail); | |
function updateUserSuccess(response) { | |
console.log(response); | |
alertService.add('success', 'User was updated.'); | |
} | |
function updateUserFail(reason) { | |
alertService.add('warning', 'User could not be updated. ' + reason); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment