【问题标题】:Angular 2: Custom Directive which detects screen resizeAngular 2:检测屏幕调整大小的自定义指令
【发布时间】:2023-04-09 12:04:01
【问题描述】:

我想创建一个可以检测浏览器高度和宽度变化的自定义指令。

我的指令:

        import { Directive, ElementRef, Input, NgZone} from '@angular/core';

        @Directive({ selector: '[resize]' })
        export class ResizeDirective {
            constructor(private ngZone: NgZone) {
                window.onresize = (e) => {
                    //ngZone.run will help to run change detection
                    this.ngZone.run(() => {
                        console.log("Width: " + window.innerWidth);
                        console.log("Height: " + window.innerHeight);
                    });
                };
            }
        }

app.module.ts:

            import { NgModule, CUSTOM_ELEMENTS_SCHEMA  } from '@angular/core';


            import { BrowserModule } from '@angular/platform-browser';
            import { ResizeDirective } from './resize.directive';

            @NgModule({
                imports: [
                    BrowserModule,
                    FormsModule,
                ],
                declarations:
                [AppComponent, ResizeDirectiv],               
                bootstrap: [AppComponent],
                schemas: [CUSTOM_ELEMENTS_SCHEMA]

            })

我已将指令放在 app.component.html 的 Body 标记中。所以每当浏览器屏幕尺寸发生变化时,我都需要进行特定的操作。

app.component.html

            <div class="outterContainer" style="width:100%;height:100%;"  resizePlugin>
            <div id="header1" class="header">


            </div>

            <div id="contentSection" class="content">
            <div>
                <div>Resize Demo</div>
                <input [(ngModel)]="studname">{{studname}}
                <createProject></createProject>
                <AddEvent></AddEvent>
                <h2>NavBar + Routing + DragAndDrop + ModalPopup + ReAgrrange Demo</h2>',

            </div>  

            <div>
                <div>
                    <h2>NavBar + Routing + DragAndDrop + ModalPopup Demo</h2>
                    <navbar></navbar>
                    <router-outlet></router-outlet>
                </div>
            </div>
            </div>
            </div>

我做错了吗?无论如何我可以修改此指令以使其侦听浏览器屏幕尺寸的变化吗?

【问题讨论】:

  • Angular 指令只能在 Angular 组件的模板中工作。 Angular 组件只能在其他 Angular 组件中使用,bootstrap: [...] 或 app-module 中列出的组件除外。
  • 嗨@GünterZöchbauer,我也尝试将指令更改为app.component.html,但它不起作用。我还有什么遗漏吗?
  • 不知道您所说的“也尝试过”是什么意思。也许您可以用您“也尝试过”的内容来更新您的问题。如果你想在不是用 Angular 构建的页面中到处添加一些 Angular 的东西,Angular 可能不适合你。
  • 你检查过指令的构造函数是否被调用了吗?
  • 您的代码看起来不错。大小已更改并显示在控制台中。 Plnkr.

标签: angular angular2-directives


【解决方案1】:

您的指令可能看起来有问题。 angular 不监听您直接传递的事件 angular 有一个装饰器用于监听@HostListener 事件。请尝试以下代码,它可能对您有所帮助

import { Directive, ElementRef, Input, NgZone,HostListener} from '@angular/core';

    @Directive({ selector: '[resize]' })
    export class ResizeDirective {
        constructor(private ngZone: NgZone) {}
        @HostListener('window:resize', ['$event'])
        onResize(event){
                //ngZone.run will help to run change detection
                this.ngZone.run(() => {
                    console.log("Width: " + window.innerWidth);
                    console.log("Height: " + window.innerHeight);
                });
        }
    }

【讨论】:

  • window:resize 替换了window.onresize 并添加了来自@angular/coreHostListener 的缺失导入,这就像一个魅力!这似乎也是“角度方式”。
【解决方案2】:

你已经在你的 app.component.html 中写了 ResizeDirectiv,但是声明的末尾多了一个 eResizeDirectiv'e' 在您的 app.module.ts 中

它们必须相同才能映射。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 2021-10-29
  • 2020-12-27
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
相关资源
最近更新 更多