2017-02-16 2 views
1

私はlist(ul)を持ち、5つのリスト要素(li)にngIf条件を追加しました。条件が満たされたときに表示されます。ngIf条件が満たされた場合や失敗した場合に、何らかのメソッドを呼び出すことができます。私はlist要素にディレクティブを追加し、ngAfterViewInit()で自分のメソッドを呼び出しました。しかし、条件が失敗したときに呼び出されることはありません。方法を提案してください。ここに私のコードがあります。angle2で* ngIf条件が失敗したときに、何らかのメソッドを呼び出す方法は?

<ul class="choose-list"> 
<li filter-selection (onWidthChange)="setfullWidth=$event" *ngIf="filter.country.name!=''"><span>{{filter.country.name}}</span><a (click)="onClearFilter('country')" href="#" class="mdi mdi-close"></a></li> 
<li filter-selection (onWidthChange)="setfullWidth=$event" *ngIf="filter.level.name!=''"><span>{{filter.level.name}}</span><a (click)="onClearFilter('level')" href="#" class="mdi mdi-close"></a></li> 
<li filter-selection (onWidthChange)="setfullWidth=$event" *ngIf="filter.institution.name!=''"><span>{{filter.institution.name}}</span><a (click)="onClearFilter('institution')" href="#" class="mdi mdi-close"></a></li> 
<li filter-selection (onWidthChange)="setfullWidth=$event" *ngIf="filter.program.name!=''"><span>{{filter.program.name}}</span><a (click)="onClearFilter('program')" href="#" class="mdi mdi-close"></a></li> 
<li filter-selection (onWidthChange)="setfullWidth=$event" *ngIf="filter.fromYear.name!=''"><span>{{filter.fromYear.name}}</span><a (click)="onClearFilter('fromYear')" href="#" class="mdi mdi-close"></a></li> 
<li filter-selection (onWidthChange)="setfullWidth=$event" *ngIf="filter.toYear.name!=''"><span>{{filter.toYear.name}}</span><a (click)="onClearFilter('toYear')" href="#" class="mdi mdi-close"></a></li> 
    </ul> 

import { Directive, HostListener, ElementRef, AfterViewInit, Input, Output , EventEmitter, Renderer } from '@angular/core'; 


@Directive({selector: '[filter-selection]'}) 
export class FilterSelectionDirective implements AfterViewInit { 

filteredElement; 
addBox; 
list; 
stretchBox; 
holder; 


@Output() onWidthChange: EventEmitter<boolean> = new EventEmitter<boolean>(); 

constructor(private elementRef: ElementRef, private renderer: Renderer) { 
    this.filteredElement = elementRef.nativeElement; 

} 


ngAfterViewInit() { 
    let ctrl = this; 
    this.addBox = this.filteredElement.parentElement.previousElementSibling; 
    this.list = this.filteredElement.parentNode; 
    this.holder = this.filteredElement.parentNode.parentNode.parentNode.parentNode; 
    this.stretchBox = this.holder.firstElementChild; 

    setTimeout(function() { 
     ctrl.refreshWidth() 
    }, 0); 
} 



refreshWidth() { 
    debugger; 
    var listWidth = 0; 

    var items = this.filteredElement.parentElement.children; // all li 

    for (var i = 0; i < items.length; i++) { 
     listWidth += $(items[i]).outerWidth(true); 
    } 
    console.log(listWidth); 

    var newWidth; 


    if (!items.length || $(this.addBox).outerWidth(true) + (listWidth + ($(this.list).outerWidth(true) - $(this.list).width())) > $(this.holder).width() * 0.7) { 
     newWidth = ''; 
     this.renderer.setElementStyle(this.stretchBox, 'width', newWidth); 
     this.onWidthChange.emit(true); //add full width 
    } 
    else { 
     this.onWidthChange.emit(false); //remove full width 
     newWidth = Math.floor((100 - (($(this.addBox).outerWidth(true) + (listWidth + 
      ($(this.list).outerWidth(true) 
       - $(this.list).width())))/$(this.holder).width() * 100)) /** 10*/) /*/ 10*/ + '%'; 

     this.renderer.setElementStyle(this.stretchBox, 'width', newWidth); 
    } 
} 
+0

直ちに更新DOMを取得するには、

this.cdRef.detectChanges(); 

を呼び出すことができます。角度変化検出は、変化検出が実行されるたびにこれらのメソッドを呼び出します。これは、条件が真/偽の間で変化するよりも頻繁に起こります。 –

+0

ありがとうございます!問題は、clearFilters()でrefrehwidth()を呼び出すときに、リスト項目がまだ削除されていないことですDOMツリーは、clearFlter()の後にレンダリングされます。 var items = this.filteredElement.parentElement.children; – Shikha

+0

'コンストラクタ(private cdRef:ChangeDetectorRef){}'を挿入し、 'this.cdRef.detectChanges() 'あなたがアイテムを削除してすぐにDOMを更新すると、' * ngFor'は表示されません。あなたのコードをあまり理解できません。レンダラーの代わりにバインディングを使用してください。 。 –

答えて

0

あなたは

constructor(private cdRef:ChangeDetectorRef){} 

を注入して、項目を削除した後、悪い考えです

+0

私はこのコンポーネントに子コンポーネントを追加すると例外 "TypeError:未定義のプロパティ 'ngDoCheck'を読み取ることができません" – Shikha

+0

申し訳ありませんが、頑張ってください。 'ngDoCheck'をどこかで呼びますか? –

+0

いいえ、this.cdRef.detectChanges()にコメントすると例外は発生しません – Shikha

関連する問題