У меня проблемы с Eventemitter Angular.@Output() public updatedIngredient = new EventEmitter();
< /code>
Функция для создания элемента следующая: < /p>
public async createIngredient(): Promise {
try {
if (this.ingredientForm.valid) {
const ingredient: Ingredient = {
name: this.ingredientForm.get('ingredientName')?.value,
description: this.ingredientForm.get('ingredientDesc')?.value,
type: this.ingredientForm.get('ingredientType')?.value,
};
await this.ingredientService.createIngredient(ingredient);
this.closeIngredientModal(true);
}
} catch (error: any) {
if (error.error.code === 'DUPLICATE_KEY') {
this.ingredientNameError = this.translateService.instant(
'APPS.YOUR_CHEF.INGREDIENT.error_existing_ingredient'
);
}
throw error;
}
}
< /code>
Функция для обновления элемента следующая: < /p>
public async updateIngredient(): Promise {
try {
if (this.ingredientForm.valid) {
const ingredientData: Partial = {
name: this.ingredientForm.get('ingredientName')?.value,
description: this.ingredientForm.get('ingredientDesc')?.value,
type: this.ingredientForm.get('ingredientType')?.value,
};
await this.ingredientService.updateIngredientById(
this.ingredient?._id || '',
ingredientData
);
this.closeIngredientModal(true);
}
} catch (error) {
throw error;
}
}
< /code>
Обе функции проходят через CloseRedIentModal, который отвечает как за закрытие модала создания /обновления элемента, так и излучения события, указывающего, был ли элемент создан или обновлен.public closeIngredientModal(createdOrUpdated: boolean) {
this.openedCreateModal = false;
this.ingredientNameError = '';
if (this.ingredient) this.initializeUpdateForm();
else this.initializeCreateForm();
this.updatedIngredient.emit(createdOrUpdated);
}
< /code>
Как вы можете видеть в этой функции, событие испускается для родителя, когда элемент был создан или обновлен, но я не знаю, почему, когда я обновляю элемент, это событие не испускается. Вот почему я не затрудняю, почему он не работает в EventeMitter, когда я обновляю элемент.>
Подробнее здесь: https://stackoverflow.com/questions/795 ... k-at-times