homework/ng1/app/modal.srv.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-04-30 20:02:47 +03:00
(function () {
'use strict';
angular.module('app').service('modalService', Srv);
Srv.$inject = ['$q', '$document', '$http', '$compile', '$rootScope'];
function Srv($q, $document, $http, $compile, $rootScope) {
this.confirm = confirm;
var modal;
var scope;
function confirm() {
if (modal) {
return showModal();
}
return $http.get('app/modal.html').then(function (result) {
createModalElement(result.data);
return showModal();
});
}
function showModal() {
var deferred = $q.defer();
scope.ok = function () {
modal.addClass('hide');
deferred.resolve();
};
scope.cancel = function () {
modal.addClass('hide');
deferred.reject();
};
modal.removeClass('hide');
return deferred.promise;
}
function createModalElement(html) {
var body = angular.element($document[0].body);
var containerElement = angular.element(html);
scope = $rootScope.$new();
modal = $compile(containerElement[0])(scope);
body.append(modal);
}
}
})();