【发布时间】:2019-08-19 21:26:12
【问题描述】:
所以基本上我有一个对象数组,我想通过@Input将数组中选定对象的索引提供给另一个组件。
The problem is, when selecting the same item twice the ngOnChanges function doesn't detect a change since it doesn't recognize it as a change when I set the value from 1 to 1... This results in me无法连续两次选择同一个对象。
子组件:
@Input('editAppointmentIndex') editAppointmentIndex: number;
ngOnChanges(changes: SimpleChanges) {
console.log(changes);
if (changes.editAppointmentIndex && changes.editAppointmentIndex.currentValue != undefined) {
// Do what i want to do with the selected object
}
}
父组件:
<child-component [editAppointmentIndex]="currentAppointmentIndex"></child-component>
currentAppointmentIndex: number;
onEdit(i) {
this.currentAppointmentIndex = i;
}
兄弟组件:
<button class="edit" (click)="onEdit(i)">Edit</button>
@Output() onEdit_: EventEmitter<number> = new EventEmitter<number>();
onEdit(i) {
this.onEdit_.emit(i);
}
【问题讨论】:
-
请提供minimal reproducible example,您说的是一个对象,但您已将
editAppointmentIndex定义为一个数字? -
@AJT_82 对不起,我刚接触堆栈溢出,给我一秒钟,我将它定义为一个数字,因为我只传递数组中对象的索引号
-
@AJT_82 希望这就够了
-
是的,这很完美:)
标签: angular typescript