简体   繁体   English

如何使用结构指令?

[英]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);
};

Running fork.跑叉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用具有多个输入的 Angular 结构指令 - How to use Angular structural directive with multiple inputs 如何在Angular中获取结构指令的ElementRef - How to get ElementRef of structural directive in Angular ngSwitch 是“属性指令”还是“结构指令”? - ngSwitch is "Attribute Directive" OR "Structural Directive" ? 在结构指令中访问 ViewChildren/ContentChildren - Accessing ViewChildren/ContentChildren in a structural Directive 在 Angular 结构指令中使用动态组件会产生额外的 HTML 标记。 如何删除或更换它? - Using dynamic component within Angular structural directive produces extra HTML tag. How to remove or replace it? 我如何在Angular2的结构指令中包含多个语句 - How can I have several statements inside a structural directive on Angular2 Angular-自定义结构指令-ngForIn:如何创建自己的局部变量? - Angular - Custom Structural Directive - ngForIn: How can I create my own local variable? 网站如何使用Javascript而不在浏览器中显示任何结构化HTML? - How can a site Use Javascript and NOT display any structural HTML in the browser? 以字符串作为输入的 angular2 结构指令 - angular2 structural directive with String as input 了解如何在指令中使用指令 - Understand how to use a directive within a directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM