简体   繁体   中英

How to use a structural directive?

I need to make a structural directive that will display the element depending on the width of the screen.

https://stackblitz.com/edit/vim8-2-resize-template-pmp1b5?file=app/shared/if-viewport-size.directive.ts

My example work, but it skip at medium size. How to fix it?

The issue is that you are overwriting the window.onresize value.

I suggest using a Subject having the onresize value then subscribe to the subject in the different directive instances.

public windowResize$ = new BehaviorSubject<number>(window.innerWidth);
window.onresize = event => {
     this.windowResize$.next(window.innerWidth);
};

Running fork.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM