<div style="position: relative; overflow:hidden"> <button ng-click="visible = true">Greeting</button> <modal visible="visible"> Hello, Habr! </modal> </div>
module.directive('modal',[ '$rootElement', function( $rootElement ){ return { restrict: 'E', ... link: function(scope, element){ element.appendTo($rootElement); scope.$on('$destroy', function(){ element.remove(); }); ... } } }] });
ng-if
directive is used together with this approach.ng-if
with a negative result of the condition completely removes the DOM element, I think many people know it, but not many know how it happens.ng-if
attribute itself.document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
comment document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
and two values are put into the block.clone
variable:ng-if
directive was declaredIn this screenshot, theng-if="!task.id"
is positive and the li element for which the directive is declared is in the DOM tree and is between the comments and. The second conditionng-if="validation.task.app_id"
is negative and there is nothing between the comments.
/** * Return the DOM siblings between the first and last node in the given array. * @param {Array} array like object * @returns {DOMElement} object containing the elements */ function getBlockElements(nodes) { var startNode = nodes[0], endNode = nodes[nodes.length - 1]; if (startNode === endNode) { return jqLite(startNode); } var element = startNode; var elements = [element]; do { element = element.nextSibling; if (!element) break; elements.push(element); } while (element !== endNode); return jqLite(elements); }
ng-if
directive and the closing comment were declared, and if the comment was not found, it will return all elements after the main element. <div id="angular-application"> ... <div style="position: relative; overflow: hidden"> <div style="position: absolute; right: 0; bottom: 0"> <modal ng-if="isFirstModal()" id="modal-1">...</modal> <modal ng-if="isSecondModal()" id="modal-2">...</modal> </div> <div style="position: absolute; left: 0; bottom: 0"> <popover ng-if="isFirstPopover()" id="popover-1">...</popover> <popover ng-if="isSecondPopover()" id="popover-2">...</popover> </div> </div> ... </div>
<div id="angular-application"> ... <div style="position: relative; overflow: hidden"> <div style="position: absolute; right: 0; bottom: 0"> <!-- ngIf: isFirstModal() --> <!-- end ngIf: isFirstModal() --> <!-- ngIf: isSecondModal() --> <!-- end ngIf: isSecondModal() --> </div> <div style="position: absolute; left: 0; bottom: 0"> <!-- ngIf: isFirstPopover() --> <!-- end ngIf: isFirstPopover() --> <!-- ngIf: isSecondPopover() --> <!-- end ngIf: isSecondPopover() --> </div> </div> ... <div id="popover-1" class="popover">...</div> <div id="modal-1" class="modal-window">...</div> <div id="modal-2" class="modal-window">...</div> <div id="popover-2" class="popover">...</div> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
.- - #popover-1 , #modal-1 , #modal-2 , #popover-2 . ng-if="isFirstPopover()"
DOM .
:
:) . . , , , , . - , - ; . , , , ; ng-if . . , . , - enable; . , ng-if
, 600; , . : element.find('[append-to-root]').appendTo($rootElement);
ng-if
, :
<div ng-if="condition"> <my-custom-directive>...</my-custom-directive> </div>
Source: https://habr.com/ru/post/225891/
All Articles