我正在使用 ngOnChanges 来检测“idCopiadora”属性的当前值和先前值。当我要求 de currectValue 总是返回 de 当前值时,没关系,但以前的值总是返回 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);
}
}
}