From 6cc717ade2275f642e4bb5f9b481e51113c6c1e5 Mon Sep 17 00:00:00 2001 From: Arthur Xavier Date: Fri, 8 Apr 2016 13:23:04 -0300 Subject: [PATCH] Make showModal fetch body element on every call to prevent missing references When the body element is changed throughout the lifecycle of an application, showModal would not append the modalElement to the body --- dst/angular-modal-service.js | 6 +++--- dst/angular-modal-service.min.js | 2 +- dst/angular-modal-service.min.js.map | 2 +- src/angular-modal-service.js | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dst/angular-modal-service.js b/dst/angular-modal-service.js index db6dbfa..eb0fe57 100644 --- a/dst/angular-modal-service.js +++ b/dst/angular-modal-service.js @@ -13,9 +13,6 @@ module.factory('ModalService', ['$animate', '$document', '$compile', '$controller', '$http', '$rootScope', '$q', '$templateRequest', '$timeout', function($animate, $document, $compile, $controller, $http, $rootScope, $q, $templateRequest, $timeout) { - // Get the body of the document, we'll add the modal to this. - var body = angular.element($document[0].body); - function ModalService() { var self = this; @@ -53,6 +50,9 @@ self.showModal = function(options) { + // Get the body of the document, we'll add the modal to this. + var body = angular.element($document[0].body); + // Create a deferred we'll resolve when the modal is ready. var deferred = $q.defer(); diff --git a/dst/angular-modal-service.min.js b/dst/angular-modal-service.min.js index ea330e5..2438684 100644 --- a/dst/angular-modal-service.min.js +++ b/dst/angular-modal-service.min.js @@ -1,3 +1,3 @@ /*angular-modal-service v0.6.10 - https://github.com/dwmkerr/angular-modal-service */ -!function(){"use strict";var e=angular.module("angularModalService",[]);e.factory("ModalService",["$animate","$document","$compile","$controller","$http","$rootScope","$q","$templateRequest","$timeout",function(e,n,l,r,t,o,c,u,a){function i(){var n=this,t=function(e,n){var l=c.defer();return e?l.resolve(e):n?u(n,!0).then(function(e){l.resolve(e)},function(e){l.reject(e)}):l.reject("No template or templateUrl has been specified."),l.promise},i=function(n,l){var r=n.children();return r.length>0?e.enter(l,n,r[r.length-1]):e.enter(l,n)};n.showModal=function(n){var u=c.defer(),p=n.controller;return p?(t(n.template,n.templateUrl).then(function(t){var p=(n.scope||o).$new(),d=c.defer(),f=c.defer(),m={$scope:p,close:function(n,l){void 0!==l&&null!==l||(l=0),a(function(){d.resolve(n),e.leave($).then(function(){f.resolve(n),p.$destroy(),m.close=null,u=null,d=null,j=null,m=null,$=null,p=null})},l)}};n.inputs&&angular.extend(m,n.inputs);var v=l(t),$=v(p);m.$element=$;var h=p[n.controllerAs],g=r(n.controller,m,!1,n.controllerAs);n.controllerAs&&h&&angular.extend(g,h),n.appendElement?i(n.appendElement,$):i(s,$);var j={controller:g,scope:p,element:$,close:d.promise,closed:f.promise};u.resolve(j)}).then(null,function(e){u.reject(e)}),u.promise):(u.reject("No controller has been specified."),u.promise)}}var s=angular.element(n[0].body);return new i}])}(); +!function(){"use strict";var e=angular.module("angularModalService",[]);e.factory("ModalService",["$animate","$document","$compile","$controller","$http","$rootScope","$q","$templateRequest","$timeout",function(e,n,l,t,r,o,c,u,a){function i(){var r=this,i=function(e,n){var l=c.defer();return e?l.resolve(e):n?u(n,!0).then(function(e){l.resolve(e)},function(e){l.reject(e)}):l.reject("No template or templateUrl has been specified."),l.promise},s=function(n,l){var t=n.children();return t.length>0?e.enter(l,n,t[t.length-1]):e.enter(l,n)};r.showModal=function(r){var u=angular.element(n[0].body),p=c.defer(),d=r.controller;return d?(i(r.template,r.templateUrl).then(function(n){var i=(r.scope||o).$new(),d=c.defer(),f=c.defer(),m={$scope:i,close:function(n,l){void 0!==l&&null!==l||(l=0),a(function(){d.resolve(n),e.leave($).then(function(){f.resolve(n),i.$destroy(),m.close=null,p=null,d=null,j=null,m=null,$=null,i=null})},l)}};r.inputs&&angular.extend(m,r.inputs);var v=l(n),$=v(i);m.$element=$;var h=i[r.controllerAs],g=t(r.controller,m,!1,r.controllerAs);r.controllerAs&&h&&angular.extend(g,h),r.appendElement?s(r.appendElement,$):s(u,$);var j={controller:g,scope:i,element:$,close:d.promise,closed:f.promise};p.resolve(j)}).then(null,function(e){p.reject(e)}),p.promise):(p.reject("No controller has been specified."),p.promise)}}return new i}])}(); //# sourceMappingURL=angular-modal-service.min.js.map diff --git a/dst/angular-modal-service.min.js.map b/dst/angular-modal-service.min.js.map index 7d5fbb4..3faebd4 100644 --- a/dst/angular-modal-service.min.js.map +++ b/dst/angular-modal-service.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["angular-modal-service.min.js","angular-modal-service.js"],"names":["module","angular","factory","$animate","$document","$compile","$controller","$http","$rootScope","$q","$templateRequest","$timeout","ModalService","self","this","getTemplate","template","templateUrl","deferred","defer","resolve","then","error","reject","promise","appendChild","parent","child","children","length","enter","showModal","options","controllerName","controller","modalScope","scope","$new","closeDeferred","closedDeferred","inputs","$scope","close","result","delay","undefined","leave","modalElement","$destroy","modal","extend","linkFn","$element","controllerObjBefore","controllerAs","modalController","appendElement","body","element","closed"],"mappings":"AAAA;AACA,CCKC,WAEC,YAEA,IAAIA,GAASC,QAAQD,OAAO,yBAE5BA,GAAOE,QAAQ,gBAAiB,WAAY,YAAa,WAAY,cAAe,QAAS,aAAc,KAAM,mBAAoB,WACnI,SAASC,EAAUC,EAAWC,EAAUC,EAAaC,EAAOC,EAAYC,EAAIC,EAAkBC,GAK9F,QAASC,KAEP,GAAIC,GAAOC,KAKPC,EAAc,SAASC,EAAUC,GACnC,GAAIC,GAAWT,EAAGU,OAalB,OAZIH,GACFE,EAASE,QAAQJ,GACRC,EACTP,EAAiBO,GAAa,GAC3BI,KAAK,SAASL,GACbE,EAASE,QAAQJ,IAChB,SAASM,GACVJ,EAASK,OAAOD,KAGpBJ,EAASK,OAAO,kDAEXL,EAASM,SAMdC,EAAc,SAASC,EAAQC,GACjC,GAAIC,GAAWF,EAAOE,UACtB,OAAIA,GAASC,OAAS,EACb1B,EAAS2B,MAAMH,EAAOD,EAAQE,EAASA,EAASC,OAAS,IAE3D1B,EAAS2B,MAAMH,EAAOD,GAG/Bb,GAAKkB,UAAY,SAASC,GAGxB,GAAId,GAAWT,EAAGU,QAGdc,EAAiBD,EAAQE,UAC7B,OAAKD,IAMLlB,EAAYiB,EAAQhB,SAAUgB,EAAQf,aACnCI,KAAK,SAASL,GAGb,GAAImB,IAAcH,EAAQI,OAAS5B,GAAY6B,OAQ3CC,EAAgB7B,EAAGU,QACnBoB,EAAiB9B,EAAGU,QACpBqB,GACFC,OAAQN,EACRO,MAAO,SAASC,EAAQC,GACRC,SAAVD,GAAiC,OAAVA,IAAgBA,EAAQ,GACnDjC,EAAS,WAEP2B,EAAclB,QAAQuB,GAGtBxC,EAAS2C,MAAMC,GACZ1B,KAAK,WAEJkB,EAAenB,QAAQuB,GAGvBR,EAAWa,WAKXR,EAAOE,MAAQ,KACfxB,EAAW,KACXoB,EAAgB,KAChBW,EAAQ,KACRT,EAAS,KACTO,EAAe,KACfZ,EAAa,QAEhBS,IAKHZ,GAAQQ,QAAQvC,QAAQiD,OAAOV,EAAQR,EAAQQ,OAInD,IAAIW,GAAS9C,EAASW,GAClB+B,EAAeI,EAAOhB,EAC1BK,GAAOY,SAAWL,CAGlB,IAAIM,GAAsBlB,EAAWH,EAAQsB,cACzCC,EAAkBjD,EAAY0B,EAAQE,WAAYM,GAAQ,EAAOR,EAAQsB,aAEzEtB,GAAQsB,cAAgBD,GAC1BpD,QAAQiD,OAAOK,EAAiBF,GAI9BrB,EAAQwB,cAEV/B,EAAYO,EAAQwB,cAAeT,GAGnCtB,EAAYgC,EAAMV,EAIpB,IAAIE,IACFf,WAAYqB,EACZnB,MAAOD,EACPuB,QAASX,EACTL,MAAOJ,EAAcd,QACrBmC,OAAQpB,EAAef,QAIzBN,GAASE,QAAQ6B,KAGlB5B,KAAK,KAAM,SAASC,GACnBJ,EAASK,OAAOD,KAGbJ,EAASM,UA9FdN,EAASK,OAAO,qCACTL,EAASM,UA9CtB,GAAIiC,GAAOxD,QAAQyD,QAAQtD,EAAU,GAAGqD,KAgJxC,OAAO,IAAI7C","file":"angular-modal-service.min.js","sourcesContent":["/*angular-modal-service v0.6.10 - https://github.com/dwmkerr/angular-modal-service */\n!function(){\"use strict\";var e=angular.module(\"angularModalService\",[]);e.factory(\"ModalService\",[\"$animate\",\"$document\",\"$compile\",\"$controller\",\"$http\",\"$rootScope\",\"$q\",\"$templateRequest\",\"$timeout\",function(e,n,r,t,l,o,c,u,a){function i(){var n=this,l=function(e,n){var r=c.defer();return e?r.resolve(e):n?u(n,!0).then(function(e){r.resolve(e)},function(e){r.reject(e)}):r.reject(\"No template or templateUrl has been specified.\"),r.promise},i=function(n,r){var t=n.children();return t.length>0?e.enter(r,n,t[t.length-1]):e.enter(r,n)};n.showModal=function(n){var u=c.defer(),p=n.controller;return p?(l(n.template,n.templateUrl).then(function(l){var p=(n.scope||o).$new(),d=c.defer(),m=c.defer(),f={$scope:p,close:function(n,r){void 0!==r&&null!==r||(r=0),a(function(){d.resolve(n),e.leave($).then(function(){m.resolve(n),p.$destroy(),f.close=null,u=null,d=null,j=null,f=null,$=null,p=null})},r)}};n.inputs&&angular.extend(f,n.inputs);var v=r(l),$=v(p);f.$element=$;var h=p[n.controllerAs],g=t(n.controller,f,!1,n.controllerAs);n.controllerAs&&h&&angular.extend(g,h),n.appendElement?i(n.appendElement,$):i(s,$);var j={controller:g,scope:p,element:$,close:d.promise,closed:m.promise};u.resolve(j)}).then(null,function(e){u.reject(e)}),u.promise):(u.reject(\"No controller has been specified.\"),u.promise)}}var s=angular.element(document.querySelector(\"body\"));return new i}])}();\n//# sourceMappingURL=angular-modal-service.min.js.map\n","// angularModalService.js\n//\n// Service for showing modal dialogs.\n\n/***** JSLint Config *****/\n/*global angular */\n(function() {\n\n 'use strict';\n\n var module = angular.module('angularModalService', []);\n\n module.factory('ModalService', ['$animate', '$document', '$compile', '$controller', '$http', '$rootScope', '$q', '$templateRequest', '$timeout',\n function($animate, $document, $compile, $controller, $http, $rootScope, $q, $templateRequest, $timeout) {\n\n // Get the body of the document, we'll add the modal to this.\n var body = angular.element($document[0].body);\n\n function ModalService() {\n\n var self = this;\n\n // Returns a promise which gets the template, either\n // from the template parameter or via a request to the\n // template url parameter.\n var getTemplate = function(template, templateUrl) {\n var deferred = $q.defer();\n if (template) {\n deferred.resolve(template);\n } else if (templateUrl) {\n $templateRequest(templateUrl, true)\n .then(function(template) {\n deferred.resolve(template);\n }, function(error) {\n deferred.reject(error);\n });\n } else {\n deferred.reject(\"No template or templateUrl has been specified.\");\n }\n return deferred.promise;\n };\n\n // Adds an element to the DOM as the last child of its container\n // like append, but uses $animate to handle animations. Returns a\n // promise that is resolved once all animation is complete.\n var appendChild = function(parent, child) {\n var children = parent.children();\n if (children.length > 0) {\n return $animate.enter(child, parent, children[children.length - 1]);\n }\n return $animate.enter(child, parent);\n };\n\n self.showModal = function(options) {\n\n // Create a deferred we'll resolve when the modal is ready.\n var deferred = $q.defer();\n\n // Validate the input parameters.\n var controllerName = options.controller;\n if (!controllerName) {\n deferred.reject(\"No controller has been specified.\");\n return deferred.promise;\n }\n\n // Get the actual html of the template.\n getTemplate(options.template, options.templateUrl)\n .then(function(template) {\n\n // Create a new scope for the modal.\n var modalScope = (options.scope || $rootScope).$new();\n\n // Create the inputs object to the controller - this will include\n // the scope, as well as all inputs provided.\n // We will also create a deferred that is resolved with a provided\n // close function. The controller can then call 'close(result)'.\n // The controller can also provide a delay for closing - this is\n // helpful if there are closing animations which must finish first.\n var closeDeferred = $q.defer();\n var closedDeferred = $q.defer();\n var inputs = {\n $scope: modalScope,\n close: function(result, delay) {\n if (delay === undefined || delay === null) delay = 0;\n $timeout(function() {\n // Resolve the 'close' promise.\n closeDeferred.resolve(result);\n\n // Let angular remove the element and wait for animations to finish.\n $animate.leave(modalElement)\n .then(function () {\n // Resolve the 'closed' promise.\n closedDeferred.resolve(result);\n\n // We can now clean up the scope\n modalScope.$destroy();\n\n // Unless we null out all of these objects we seem to suffer\n // from memory leaks, if anyone can explain why then I'd\n // be very interested to know.\n inputs.close = null;\n deferred = null;\n closeDeferred = null;\n modal = null;\n inputs = null;\n modalElement = null;\n modalScope = null;\n });\n }, delay);\n }\n };\n\n // If we have provided any inputs, pass them to the controller.\n if (options.inputs) angular.extend(inputs, options.inputs);\n\n // Compile then link the template element, building the actual element.\n // Set the $element on the inputs so that it can be injected if required.\n var linkFn = $compile(template);\n var modalElement = linkFn(modalScope);\n inputs.$element = modalElement;\n\n // Create the controller, explicitly specifying the scope to use.\n var controllerObjBefore = modalScope[options.controllerAs];\n var modalController = $controller(options.controller, inputs, false, options.controllerAs);\n\n if (options.controllerAs && controllerObjBefore) {\n angular.extend(modalController, controllerObjBefore);\n }\n\n // Finally, append the modal to the dom.\n if (options.appendElement) {\n // append to custom append element\n appendChild(options.appendElement, modalElement);\n } else {\n // append to body when no custom append element is specified\n appendChild(body, modalElement);\n }\n\n // We now have a modal object...\n var modal = {\n controller: modalController,\n scope: modalScope,\n element: modalElement,\n close: closeDeferred.promise,\n closed: closedDeferred.promise\n };\n\n // ...which is passed to the caller via the promise.\n deferred.resolve(modal);\n\n })\n .then(null, function(error) { // 'catch' doesn't work in IE8.\n deferred.reject(error);\n });\n\n return deferred.promise;\n };\n\n }\n\n return new ModalService();\n }]);\n\n}());\n"],"sourceRoot":"/source/"} \ No newline at end of file +{"version":3,"sources":["angular-modal-service.min.js","angular-modal-service.js"],"names":["module","angular","factory","$animate","$document","$compile","$controller","$http","$rootScope","$q","$templateRequest","$timeout","ModalService","self","this","getTemplate","template","templateUrl","deferred","defer","resolve","then","error","reject","promise","appendChild","parent","child","children","length","enter","showModal","options","body","element","controllerName","controller","modalScope","scope","$new","closeDeferred","closedDeferred","inputs","$scope","close","result","delay","undefined","leave","modalElement","$destroy","modal","extend","linkFn","$element","controllerObjBefore","controllerAs","modalController","appendElement","closed"],"mappings":"AAAA;AACA,CCKC,WAEC,YAEA,IAAIA,GAASC,QAAQD,OAAO,yBAE5BA,GAAOE,QAAQ,gBAAiB,WAAY,YAAa,WAAY,cAAe,QAAS,aAAc,KAAM,mBAAoB,WACnI,SAASC,EAAUC,EAAWC,EAAUC,EAAaC,EAAOC,EAAYC,EAAIC,EAAkBC,GAE9F,QAASC,KAEP,GAAIC,GAAOC,KAKPC,EAAc,SAASC,EAAUC,GACnC,GAAIC,GAAWT,EAAGU,OAalB,OAZIH,GACFE,EAASE,QAAQJ,GACRC,EACTP,EAAiBO,GAAa,GAC3BI,KAAK,SAASL,GACbE,EAASE,QAAQJ,IAChB,SAASM,GACVJ,EAASK,OAAOD,KAGpBJ,EAASK,OAAO,kDAEXL,EAASM,SAMdC,EAAc,SAASC,EAAQC,GACjC,GAAIC,GAAWF,EAAOE,UACtB,OAAIA,GAASC,OAAS,EACb1B,EAAS2B,MAAMH,EAAOD,EAAQE,EAASA,EAASC,OAAS,IAE3D1B,EAAS2B,MAAMH,EAAOD,GAG/Bb,GAAKkB,UAAY,SAASC,GAGxB,GAAIC,GAAOhC,QAAQiC,QAAQ9B,EAAU,GAAG6B,MAGpCf,EAAWT,EAAGU,QAGdgB,EAAiBH,EAAQI,UAC7B,OAAKD,IAMLpB,EAAYiB,EAAQhB,SAAUgB,EAAQf,aACnCI,KAAK,SAASL,GAGb,GAAIqB,IAAcL,EAAQM,OAAS9B,GAAY+B,OAQ3CC,EAAgB/B,EAAGU,QACnBsB,EAAiBhC,EAAGU,QACpBuB,GACFC,OAAQN,EACRO,MAAO,SAASC,EAAQC,GACRC,SAAVD,GAAiC,OAAVA,IAAgBA,EAAQ,GACnDnC,EAAS,WAEP6B,EAAcpB,QAAQyB,GAGtB1C,EAAS6C,MAAMC,GACZ5B,KAAK,WAEJoB,EAAerB,QAAQyB,GAGvBR,EAAWa,WAKXR,EAAOE,MAAQ,KACf1B,EAAW,KACXsB,EAAgB,KAChBW,EAAQ,KACRT,EAAS,KACTO,EAAe,KACfZ,EAAa,QAEhBS,IAKHd,GAAQU,QAAQzC,QAAQmD,OAAOV,EAAQV,EAAQU,OAInD,IAAIW,GAAShD,EAASW,GAClBiC,EAAeI,EAAOhB,EAC1BK,GAAOY,SAAWL,CAGlB,IAAIM,GAAsBlB,EAAWL,EAAQwB,cACzCC,EAAkBnD,EAAY0B,EAAQI,WAAYM,GAAQ,EAAOV,EAAQwB,aAEzExB,GAAQwB,cAAgBD,GAC1BtD,QAAQmD,OAAOK,EAAiBF,GAI9BvB,EAAQ0B,cAEVjC,EAAYO,EAAQ0B,cAAeT,GAGnCxB,EAAYQ,EAAMgB,EAIpB,IAAIE,IACFf,WAAYqB,EACZnB,MAAOD,EACPH,QAASe,EACTL,MAAOJ,EAAchB,QACrBmC,OAAQlB,EAAejB,QAIzBN,GAASE,QAAQ+B,KAGlB9B,KAAK,KAAM,SAASC,GACnBJ,EAASK,OAAOD,KAGbJ,EAASM,UA9FdN,EAASK,OAAO,qCACTL,EAASM,UAkGtB,MAAO,IAAIZ","file":"angular-modal-service.min.js","sourcesContent":["/*angular-modal-service v0.6.10 - https://github.com/dwmkerr/angular-modal-service */\n!function(){\"use strict\";var e=angular.module(\"angularModalService\",[]);e.factory(\"ModalService\",[\"$animate\",\"$document\",\"$compile\",\"$controller\",\"$http\",\"$rootScope\",\"$q\",\"$templateRequest\",\"$timeout\",function(e,n,l,r,t,o,c,u,a){function i(){var n=this,t=function(e,n){var l=c.defer();return e?l.resolve(e):n?u(n,!0).then(function(e){l.resolve(e)},function(e){l.reject(e)}):l.reject(\"No template or templateUrl has been specified.\"),l.promise},i=function(n,l){var r=n.children();return r.length>0?e.enter(l,n,r[r.length-1]):e.enter(l,n)};n.showModal=function(n){var u=c.defer(),p=n.controller;return p?(t(n.template,n.templateUrl).then(function(t){var p=(n.scope||o).$new(),d=c.defer(),f=c.defer(),m={$scope:p,close:function(n,l){void 0!==l&&null!==l||(l=0),a(function(){d.resolve(n),e.leave($).then(function(){f.resolve(n),p.$destroy(),m.close=null,u=null,d=null,j=null,m=null,$=null,p=null})},l)}};n.inputs&&angular.extend(m,n.inputs);var v=l(t),$=v(p);m.$element=$;var h=p[n.controllerAs],g=r(n.controller,m,!1,n.controllerAs);n.controllerAs&&h&&angular.extend(g,h),n.appendElement?i(n.appendElement,$):i(s,$);var j={controller:g,scope:p,element:$,close:d.promise,closed:f.promise};u.resolve(j)}).then(null,function(e){u.reject(e)}),u.promise):(u.reject(\"No controller has been specified.\"),u.promise)}}var s=angular.element(n[0].body);return new i}])}();\n//# sourceMappingURL=angular-modal-service.min.js.map\n","// angularModalService.js\n//\n// Service for showing modal dialogs.\n\n/***** JSLint Config *****/\n/*global angular */\n(function() {\n\n 'use strict';\n\n var module = angular.module('angularModalService', []);\n\n module.factory('ModalService', ['$animate', '$document', '$compile', '$controller', '$http', '$rootScope', '$q', '$templateRequest', '$timeout',\n function($animate, $document, $compile, $controller, $http, $rootScope, $q, $templateRequest, $timeout) {\n\n function ModalService() {\n\n var self = this;\n\n // Returns a promise which gets the template, either\n // from the template parameter or via a request to the\n // template url parameter.\n var getTemplate = function(template, templateUrl) {\n var deferred = $q.defer();\n if (template) {\n deferred.resolve(template);\n } else if (templateUrl) {\n $templateRequest(templateUrl, true)\n .then(function(template) {\n deferred.resolve(template);\n }, function(error) {\n deferred.reject(error);\n });\n } else {\n deferred.reject(\"No template or templateUrl has been specified.\");\n }\n return deferred.promise;\n };\n\n // Adds an element to the DOM as the last child of its container\n // like append, but uses $animate to handle animations. Returns a\n // promise that is resolved once all animation is complete.\n var appendChild = function(parent, child) {\n var children = parent.children();\n if (children.length > 0) {\n return $animate.enter(child, parent, children[children.length - 1]);\n }\n return $animate.enter(child, parent);\n };\n\n self.showModal = function(options) {\n\n // Get the body of the document, we'll add the modal to this.\n var body = angular.element($document[0].body);\n\n // Create a deferred we'll resolve when the modal is ready.\n var deferred = $q.defer();\n\n // Validate the input parameters.\n var controllerName = options.controller;\n if (!controllerName) {\n deferred.reject(\"No controller has been specified.\");\n return deferred.promise;\n }\n\n // Get the actual html of the template.\n getTemplate(options.template, options.templateUrl)\n .then(function(template) {\n\n // Create a new scope for the modal.\n var modalScope = (options.scope || $rootScope).$new();\n\n // Create the inputs object to the controller - this will include\n // the scope, as well as all inputs provided.\n // We will also create a deferred that is resolved with a provided\n // close function. The controller can then call 'close(result)'.\n // The controller can also provide a delay for closing - this is\n // helpful if there are closing animations which must finish first.\n var closeDeferred = $q.defer();\n var closedDeferred = $q.defer();\n var inputs = {\n $scope: modalScope,\n close: function(result, delay) {\n if (delay === undefined || delay === null) delay = 0;\n $timeout(function() {\n // Resolve the 'close' promise.\n closeDeferred.resolve(result);\n\n // Let angular remove the element and wait for animations to finish.\n $animate.leave(modalElement)\n .then(function () {\n // Resolve the 'closed' promise.\n closedDeferred.resolve(result);\n\n // We can now clean up the scope\n modalScope.$destroy();\n\n // Unless we null out all of these objects we seem to suffer\n // from memory leaks, if anyone can explain why then I'd\n // be very interested to know.\n inputs.close = null;\n deferred = null;\n closeDeferred = null;\n modal = null;\n inputs = null;\n modalElement = null;\n modalScope = null;\n });\n }, delay);\n }\n };\n\n // If we have provided any inputs, pass them to the controller.\n if (options.inputs) angular.extend(inputs, options.inputs);\n\n // Compile then link the template element, building the actual element.\n // Set the $element on the inputs so that it can be injected if required.\n var linkFn = $compile(template);\n var modalElement = linkFn(modalScope);\n inputs.$element = modalElement;\n\n // Create the controller, explicitly specifying the scope to use.\n var controllerObjBefore = modalScope[options.controllerAs];\n var modalController = $controller(options.controller, inputs, false, options.controllerAs);\n\n if (options.controllerAs && controllerObjBefore) {\n angular.extend(modalController, controllerObjBefore);\n }\n\n // Finally, append the modal to the dom.\n if (options.appendElement) {\n // append to custom append element\n appendChild(options.appendElement, modalElement);\n } else {\n // append to body when no custom append element is specified\n appendChild(body, modalElement);\n }\n\n // We now have a modal object...\n var modal = {\n controller: modalController,\n scope: modalScope,\n element: modalElement,\n close: closeDeferred.promise,\n closed: closedDeferred.promise\n };\n\n // ...which is passed to the caller via the promise.\n deferred.resolve(modal);\n\n })\n .then(null, function(error) { // 'catch' doesn't work in IE8.\n deferred.reject(error);\n });\n\n return deferred.promise;\n };\n\n }\n\n return new ModalService();\n }]);\n\n}());\n"],"sourceRoot":"/source/"} \ No newline at end of file diff --git a/src/angular-modal-service.js b/src/angular-modal-service.js index db6dbfa..eb0fe57 100644 --- a/src/angular-modal-service.js +++ b/src/angular-modal-service.js @@ -13,9 +13,6 @@ module.factory('ModalService', ['$animate', '$document', '$compile', '$controller', '$http', '$rootScope', '$q', '$templateRequest', '$timeout', function($animate, $document, $compile, $controller, $http, $rootScope, $q, $templateRequest, $timeout) { - // Get the body of the document, we'll add the modal to this. - var body = angular.element($document[0].body); - function ModalService() { var self = this; @@ -53,6 +50,9 @@ self.showModal = function(options) { + // Get the body of the document, we'll add the modal to this. + var body = angular.element($document[0].body); + // Create a deferred we'll resolve when the modal is ready. var deferred = $q.defer();