[英]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 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.问题是您正在覆盖
window.onresize
值。
I suggest using a Subject
having the onresize
value then subscribe to the subject in the different directive instances.我建议使用具有
onresize
值的Subject
,然后订阅不同指令实例中的主题。
public windowResize$ = new BehaviorSubject<number>(window.innerWidth);
window.onresize = event => {
this.windowResize$.next(window.innerWidth);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.