U
U
Urukhayy2017-01-18 19:36:06
Angular
Urukhayy, 2017-01-18 19:36:06

How to get data from a child component on demand of an ancestor component?

It is necessary, when a button is pressed from the ancestor component, to take JSON data, which is a field in the class of the child component, which in turn gets there through input.
What is the best way to implement this on input/output?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zekohina, 2017-01-18
@Urukhayy

1) Write data to the service, and take it from there.
2) Send an event from top to bottom, and if there is data, then send an event back up, with data.

L
lega, 2017-01-19
@lega

Passing through a service is not always the best way.
For example, the video component, will you call the play() and pause() commands through the service? This is done directly.
And the input element, in order to get input.value, will you also start a service for this and store links to all input components? It is in redux that data is driven in a large circle, and even then not always.
You can send an event - which is a crutch, you can make a "publication" api, there are examples on the Internet, in other frameworks it is out of the box, alight example: https://jsfiddle.net/lega911/kr1hwoj6/

V
Vitaly, 2017-01-18
@vitali1995

All such actions in Angular are done through services.
For example, a setter is hung on input, which, after assignment, duplicates the value on the context service, which is then injected into child components.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question