当* ngIf条件在angular2中失败时,如何调用某些方法?

时间:2017-02-16 06:12:50

标签: angular

我有列表(ul)并在5个列表元素(li)上添加了ngIf条件。它们将在满足条件时显示。我想在满足或失败条件时调用某些方法。我在list元素上添加了指令并在ngAfterViewInit()中调用了我的方法。但是当条件失败时,它不会被调用。请建议任何出路。这是我的一段代码。

<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);
    }
}

1 个答案:

答案 0 :(得分:0)

你可以注入

constructor(private cdRef:ChangeDetectorRef){}

并致电

this.cdRef.detectChanges();

删除项目后,立即更新DOM