S
S
Sergey Pashkevich2015-08-26 00:35:24
Angular
Sergey Pashkevich, 2015-08-26 00:35:24

How to "correctly" pass data to the parent controller from the child?

Good time to all.
The question arose about the most correct approach to developing an application on angular.js, so that in the future you would not have to suffer very much with the support of the application and not change many things.
The project uses ui-router for states.
The application has the following hierarchy:
- root (root)
- home (root.home)
- sale (root.sale)
- list (root.sale.list)
- single (root.sale.single)
The root module is the global module of the entire project ( where it will be possible to take out generalized events / actions for the entire project).
The home and sale modules are two independent pages and they have different page structures, but the template for the sale page is very identical to many other pages, and it was decided to make this page another ancestor and put everything common to all child pages (header, footer and content) divided into two parts: statics and a block where child pages will be loaded). Now on the sale page there is a block to which you need to pass data from all child pages, how best to do this using ui-router or more elegant methods, so that it would not be very painful to fix it in the future)
Below is an example of what I wrote above ( the $emit & $on events hung on $rootScope are used for passing):
Example
Is it good to use this approach with nesting and the root module root?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-26
@siarheipashkevich

the right thing to do is not to pass them up at all. It's a little more than strange that a parent knows about the details of the children. He doesn't need to know anything about them. For what you want to do, nested views have been introduced.
events are cool and convenient, but it's very easy to turn everything into unsupported trash once the system grows to any large scale. To tie them to some kind of logic is generally a disastrous business. In extreme cases, you have two ways:
- a key in the form of data in the parent state (the data of the state is inherited, more details in the documentation)
- have a service that will have up-to-date information

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question