Angular 4:NgOnchanges不起作用,previousValue总是未定义的

时间:2017-11-06 11:35:41

标签: angular ngonchanges

我正在使用ngOnChanges来检测de current值和“idCopiadora”属性的先前值。当我要求de currectValue时总是返回当前值,这没关系,但前一个值总是返回undefined 我看不出有什么问题,请帮忙。

组件父.html:

<app-gestioncopiadora-ficha *ngIf="selectedId !== 0" [idCopiadora] ="selectedId" (onClosed)="closeModal($event)"></app-gestioncopiadora-ficha>
<div [class] = "selectedId === 0? '':'hide'">
    <div style="display:table;width:100%" class="listadoTabla" [@fadeInAnimation]="animationEnabled" (@fadeInAnimation.done)="animationEnabled = false">
        <div *ngFor="let fila of tempRows" style="display:inline-block; width:100%;" class="listadoFila project-view-item" (click)="selectFila(fila.Id)">
            <div *ngIf="fila" style="width:100%;">
                <div style="display:inline-block;padding-left: 5px;">
                    <i class="glyphicon glyphicon-chevron-down"></i>
                </div>
                <div style="display:inline-block;width:25%;padding-left: 5px;">
                    <span *ngIf="fila.CodigoCopiadora">{{fila.CodigoCopiadora}} - </span>
                    <span class="negrita600">{{fila.Marca}}</span>
                    <span class="negrita600">{{fila.Modelo}}</span>       
                </div>
            </div>
        </div>
    </div>
</div>

组件父亲.ts:

//... someStuf:
selectFila(_id: any) {
    this.selectedId = _id;       
}

组件子:

import { Component, OnDestroy, OnInit, SimpleChanges, Input, OnChanges, 
SimpleChange } from '@angular/core';
@Component({
    selector: 'app-gestioncopiadora-ficha',
    template: `{{idCopiadora}}`,
    styleUrls: ['./cuerpo.component.css'],
    inputs: ["idCopiadora"],
})
export class CuerpoComponent implements OnChanges {
    constructor() {       
    }
     @Input() idCopiadora: number;

     ngOnChanges(changes: SimpleChanges ) {
        for (let propertyName in changes) {
            let change = changes[propertyName];
            let current = JSON.stringify(change.currentValue);
            let previous = JSON.stringify(change.previousValue);
            console.log('prev value: ', current);
            console.log('got name: ', previous);
        }
    }
}

0 个答案:

没有答案