, , ,
-, , , , , .
, , $compile , , .
:
angular.module("app", [])
.factory("DialogService", function($q, $compile){
return {
toast:function(text){
Dialog.toast(text);
},
alert:function(text){
var deferred = $q.defer();
var d = Dialog.alert(text);
d.bind("hide", function(){
deferred.resolve();
});
return deferred.promise;
},
showModal:function(options){
var childScope;
var dialog = new Dialog(options);
dialog.show(function(){
var self = this;
if ( "scope" in options ){
var childScope = options.scope.$new();
$compile(self.contentLayer)(childScope);
options.scope.dismiss = function(){
dialog.hide();
}
dialog.bind("hide", function(){
childScope.$destroy();
});
}
});
if ( "hide" in options ){
dialog.bind("hide", options.hide);
}
},
confirm:function(text){
var deferred = $q.defer();
Dialog.confirm(text, function(){
deferred.resolve();
}, function(){
deferred.reject();
});
return deferred.promise;
}
}
})
;
Extra
, , , , , .
, , ; , , :
var watchers = [
$scope.$watch(...),
$scope.$watch(...),
...
];
...
var observer = new MutationObserver(function(mutations) {
if (!document.body.contains($element[0])){
observer.disconnect();
dropdown.remove();
for ( var i = 0; i < watchers.length; i++ ){
watchers[i]();
}
$scope.$destroy();
return;
}
});
var config = { childList: true, subtree: false };
observer.observe(document.querySelector('body'), config);
, ? ... , - ( ):
var clickHandler = function(event){
var isChild = $($element).has(event.target).length > 0 || $(dropdown).has(event.target).length > 0;
var isSelf = $element[0] == event.target || dropdown == event.target;
$scope.$apply(function(){
if (!isChild && !isSelf) {
$scope.mdSelectCtrl.dismiss();
}
});
}
$document.bind('click', clickHandler);
click $, click ( , ); , 10-15 , , $ , , , .
: DOM, :
var observer = new MutationObserver(function(mutations) {
if (!document.body.contains($element[0])){
observer.disconnect();
dropdown.remove();
for ( var i = 0; i < watchers.length; i++ ){
watchers[i]();
}
$scope.$destroy();
$document.unbind('click', clickHandler);
return;
}
});
, , , MutationObserver , , node, :
$scope.$on("destroy")
- , .