Reputation: 1699
I needed to use an suitable alert and helper for my project and found angular material to be awesome. However instead of pasting in the few lines in each controller because I needed to reuse them.
Upvotes: 1
Views: 1678
Reputation: 1699
I needed to set these up as a factory so I can call them from any controller. I find them very helpful might be of use to someone.
Alert
(function () {
'use strict';
app.factory("showAlert", ["$mdDialog", function ($mdDialog) {
return function (title, content, ev) {
$mdDialog.show(
$mdDialog.alert()
.parent(angular.element(document.querySelector('#popupContainer')))
.clickOutsideToClose(true)
.title(title)
.textContent(content)
.ok('Ok')
.targetEvent(ev));
};
}]);
})();
app.controller('someController', showAlert) {
$scope.testAlert = function(event)
{
showAlert('Alert Title Goes Here', 'This is the alert message body.', ev);
}
}
Information Helper
(function () {
'use strict';
app.factory("showHelper", ["$mdToast", "$timeout", function ($mdToast, $timeout) {
return function (content, startTime, duration) {
$timeout(function () {
$mdToast.show(
$mdToast.simple()
.textContent(content)
.position('bottom left')
.hideDelay(duration * 1000)
);
}, startTime * 1000);
};
}]);
})();
app.controller('someController', showHelper) {
$scope.testAlert = function()
{
showHelper('I am the first helper', 1, 4);
showHelper('I am the second helper', 6, 2);
}
}
Upvotes: 3