Возможно ли иметь обратный вызов с @Output
?
У меня есть FormComponent
, который проверяет действительность и отключает кнопку отправки при отправке.Теперь я хотел бы снова включить кнопку отправки, когда отправка завершится.
@Component({
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
...
</form>
`
})
class FormComponent {
form: FormGroup = ...;
isSubmitting = false;
@Output()
submitted = new EventEmitter<MyData>()
onSubmit() {
if(this.form.invalid || this.isSubmitting) {
return;
}
this.isSubmitting = true;
this.submitted.emit(this.form.value);
// Here I'd like to listen for the result of the parent component
// something like this...
// this.submitted.emit(...).subscribe(res => this.isSubmitting = false);
}
}
@Component({
template: `
<my-form (submitted)="onSubmitted($event)"></my-form>
`
})
class ParentComponent {
constructor(private service: MyService) { }
onSubmitted(event: MyData) {
this.service.doSomething(event).pipe(
tap(res => console.log("service res", res)
);
// basically I'd like to `return` this `Observable`,
// so the `FormComponent` can listen for the completion
}
}
Я знаю, я мог бы использовать @Input()
в FormComponent
и сделать что-то вроде этого:
@Input()
set submitted(val: boolean) {
this.isSubmitted = val;
}
Но я хотел бы знать, есть ли более простое / лучшее решение, потому что isSubmitted
должно быть внутренним свойством FormComponent
, которое должно управляться самим компонентом, а не его родителем.