2017-05-22 11 views
0

ブラウザの高さと幅の変化を検出できるカスタムディレクティブを作成したいとします。角2:画面のサイズ変更を検出するカスタムディレクティブ

マイ指令:

 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> 

アム私が何か間違ったことをやって?とにかく、このディレクティブを変更してブラウザの画面サイズの変更を聞くことができますか?

+1

角型ディレクティブは、角型コンポーネントのテンプレート内でのみ機能します。角型コンポーネントは 'bootstrap:[...]'またはapp-moduleにリストされているものを除き、他のAngularコンポーネント内でのみ動作します。 –

+0

こんにちは@GünterZöchbauer、私はまた、app.component.htmlへのディレクティブを変更しようとしましたが、動作しません。他に何かが足りないのですか? –

+0

「やったことがありますか」という意味が分かりません。おそらくあなたもあなたが「やった」とあなたの質問を更新することができます。 Angularで構築されていないページにAngularのものを追加したい場合、Angularはおそらくあなたにとって正しいフレームワークではありません。 –

答えて

0

あなたの指示に問題があるように見えるかもしれません。 angleは、あなたが直接渡したイベントをリッスンしません。@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); 
       }); 
     } 
    } 
+0

'window.onresize'を' window:resize'に置き換え、 '@ angle/core'から' HostListener'の欠落したインポートを追加しました。これは魅力的です!これはまた、「角度のある道」と思われる。ありがとう@lentschi。 – lentschi

+0

私は私の答えを更新しました –

関連する問題