2017-09-04 5 views
1

特定のクラスが追加されたときにイオンアイテムスライディング要素をトリガーします。私の場合、「アクティブスライド」。 ViewChildで要素を参照しようとしましたが、常にエラーが発生します。ViewChildは動作していません(イオン要素)

TypeError: Cannot read property 'classList' of undefined 

clickHandlerメソッドが呼び出されたとき。デフォルトで

<ion-list> 
    <ion-item-sliding #slideItem> 
    <ion-item> 
     Item 
    </ion-item> 
    <ion-item-options side="left"> 
     <button ion-button (click)="favorite(item)">Favorite</button> 
     <button ion-button color="danger" (click)="share(item)">Share</button> 
    </ion-item-options> 

    <ion-item-options side="right"> 
     <button ion-button (click)="unread(item)">Unread</button> 
    </ion-item-options> 
    </ion-item-sliding> 
</ion-list> 

HTML

export class TestPage { 

    @ViewChild('slideItem') slideItem : ElementRef; 

    constructor() {} 

    // ... 

    @HostListener('click', ['$event']) 
    clickHandler(event) { 
    console.log("Test 1"); // gets called 

    if(this.slideItem.nativeElement.classList.contains('active-slide')) { 
     console.log("Test 2"); 
    } 
    } 
} 

答えて

4

@ViewChild()要素、コンポーネントまたはディレクティブをホストするとき代わりElementRefのコンポーネントインスタンスを返します。あなたが明示的ElementRef

@ViewChild('slideItem', {read: ElementRef}) slideItem : ElementRef; 

をしたいと述べている必要があり

angular 2/typescript : get hold of an element in the template

+1

感謝を参照してください。恥のように働く。 –

関連する問題