На самом деле все ваши notificationArray
элементы - это один и тот же экземпляр newNotification
.Делая это: this.newNotification = {thresholdId: "0", notificationLevel: "", message: ""};
вы фактически сбрасываете первую строку из-за этого.
Попробуйте вместо этого сохранить newNotification
в переменной:
notificationArray: Array<NotificationLevel> = [];
newNotification: any = {};
ngOnInit(): void {
var newNotification = {thresholdId: "0", notificationLevel: "", message: ""};
this.notificationArray.push(newNotification);
}
addNotification(index) {
console.log(this.notificationArray);
// here is the correction
var newNotification = {thresholdId: "0", notificationLevel: "", message: ""};
this.notificationArray.push(newNotification);
console.log(this.notificationArray);
return true;
}
deleteNotification(index) {
if(this.notificationArray.length ==1) {
alert("At least one Notification Required for an Inspection");
return false;
} else {
this.notificationArray.splice(index, 1);
return true;
}
}
update:
Я не знаю, связано ли это, но у вас будет дубликат threshold
id на ваш выбор.Используйте свой i
, чтобы сделать его уникальным.То же самое касается ваших входных имен.Вы должны добавить []
после имени поля, иначе будет отправлена только последняя строка.
Я добавил console.log
вашего массива после нажатия.Можете ли вы сказать в комментарии, сбрасывается ли первый индекс в этот момент?