A
A
Alena Khrenovskaya2018-08-21 15:06:25
JavaScript
Alena Khrenovskaya, 2018-08-21 15:06:25

How to pass form TD data (or form itself) from child component to parent?

There is a parent component with a Submit button and a child component with a form (template driven forms).
Well, the question is: How to send the form itself or form.value from the child component to the parent
) but this is necessary for further formation of the model and sending it to the database)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-08-21
@ally69

parent

<app-child #appChild (submit)="onSubmit($event)"></app-child>
<button (click)="goMessage()">send</button>

@ViewChild('appChild') appChild: ChildComponent;

/** вызов метода потомка */
goMessage() {
  this.appChild.send('hello from parent');
}

/** реакция на событие из потомка */
onSubmit(event) {
  // проверка данных, заполнение полей, whatever
}

child of ChildComponent
@Output() submit = new EventEmitter<string>();

/** отправим полученную строку обратно наверх */
public send(message: string) {
  this.submit.emit(message);
}

You can also send "events" through the usual @Input props by feeding it a new object or a random number, i.e. so that the value of the prop differs from the previous one. And catch changes with a setter or in ngOnChanges

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question