17212

Listen to element visibility in Angular 2

I'm using Bootstrap and Angular 2 (v4) for my webapp. I would like to listen to an element in a directive for visibility changes. My element has a parent that can hide its children with hidden-sm-up and I need to trigger a function each time it is hidden or displayed.

div.hidden-sm-up input.form-control(myDirective, type='number')

and in my directive:

@Directive({ selector: '[myDirective]' }) export class myDirective { constructor(private element: ElementRef, private renderer: Renderer){ } ngAfterViewInit(): void{ // listen to visibility change here } }

Answer1:

ngDoCheck is run when change detection is run, therefore I think it's the right place if you want to monitor it instead of just get it once at component creation time:

@HostListener('window:resize') ngDoCheck() { this.isVisible = this.element.nativeElement.offsetParent !== null; }

There might be better option, like some events that are emitted somewhere, but for the general case ngDoCheck should work fine.

Recommend

  • Angular5 Service Worker update(SWUpdate) not detecting on firefox. Working on chrome
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • How to replace TouchesBegan with UIGestureRecognizer
  • get passwords from chrome
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • css background images not always displayed
  • You tube videos are not playing
  • Jquery popup on mouse over of calendar control
  • AJAX Html Editor Extender upload image appearing blank
  • FileReader+canvas image loading problem
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Insert into database using onclick function
  • QLineEdit password safety
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Bug in WPF DataGrid
  • Window Size for Mac application
  • Does CUDA 5 support STL or THRUST inside the device code?
  • what is the difference between the asp.net mvc application and asp.net web application
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Matrix multiplication with MKL
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?