Is there a way in Angular2 to have an event fired when my component becomes visible ? It is placed in a tabcontrol and I want to be notified when the user switches I'd like my component to fire an event.
相关问题
- Angular RxJS mergeMap types
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Ignore Typescript errors in Webpack-dev-server
- How to update placeholder text in ng2-smart-table?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Cannot find module 'redux' 怎么解决?
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- What does it means in C# : using -= operator by ev
There is no such event, but if you're using a tab control, the proper way to do this would be to create a tab change
@Output
for your tab control if it's custom, otherwise, most tab controls (like ng-bootstrap) have some tab change event as well.If your component has to be aware of this, you can use this tab change event to detect which tab is visible, and if you know which tab is visible, you also know if your component is visible or not. So you can do something like this:
And then you can send it to your component itself if you have an input:
And then you can apply it with:
Now you can listen to
OnChanges
to see if the model valueactivated
changed totrue
.You can also refactor this to use a service with an
Observable
like this:When a component wishes to listen to these changes, it can subscribe to
tabService.observable
. When your tab changes, you can push new items to it withtabService.observer.next()
.For those watching at home, you can now use
ngAfterContentInit()
for this, at least on Ionic anyway. https://angular.io/guide/lifecycle-hooksYou can use the
ngAfterViewInit()
callbackhttps://angular.io/docs/ts/latest/guide/lifecycle-hooks.html
Update
The new Intersection Observer API can be used for that https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API See also https://stackoverflow.com/a/44670818/217408
What I finally did (which is not very beautiful but works while I don't have a better way to do it...) is to use the
ngAfterContentChecked()
callback and handle the change myself.