interface MutationWindow extends Window { MutationObserver : any ; WebKitMutationObserver : any ; } declare var window ; import { Directive , Inject , ElementRef , forwardRef , OnDestroy , AfterViewInit } from '@angular/core' ; import { AngularMasonryComponent } from './app-masonry.component' ; @ Directive ( { selector : '[masonry-brick], masonry-brick' } ) export class AngularMasonryBrickDirective implements OnDestroy , AfterViewInit { constructor ( private _element , @ Inject ( forwardRef ( ( ) => AngularMasonryComponent ) ) private _parent ) { } ngAfterViewInit ( ) { this . _parent . add ( this . _element . nativeElement ) ; this . watchForHtmlChanges ( ) ; } ngOnDestroy ( ) { this . _parent . remove ( this . _element . nativeElement ) ; } private watchForHtmlChanges ( ) { MutationObserver = window . MutationObserver || window . WebKitMutationObserver ; if ( MutationObserver ) { const self = this ; const observer = new MutationObserver ( function ( mutations , observerFromElement ) { self . _parent . layout ( ) ; } ) ; observer . observe ( this . _element . nativeElement , { subtree : true , childList : true } ) ; } } }