D
D
DieZz2015-10-16 15:09:14
Angular
DieZz, 2015-10-16 15:09:14

How to properly pass data between controllers in AngularJs?

In an application on Angular I use ui-router. Roughly speaking, the main screen is divided into two parts: sideBar and main. Example:

.state('node', {
                url: "/node",
                views: {
                    "main" : {
                        templateUrl: '/templates/node/node.html',
                        controller: 'NodeController',
                        }
                    },
                    "sideBar " : {
                        templateUrl : '/templates/sideBar.html',
                        controller: 'sideBarController',
                    }
                }
            })

View:main displays a table, each row of which is an entry in the "node" database table (NodeController.nodes). In view:sideBar there is a button with a call to a modal window, with which you can add a new line to the table on the server. The server responds with the newly created record if everything went well.
Question: how to do it correctly so that after adding to the database through a modal window, add a new entry to NodeController.nodes.
I have two options:
1. Create a NodeService with nodes variable. Write all entries to this variable, add from the modal window and, using $scope.$watch in the NodeController, track changes.
2. In sideBarController use $rootScope.$emit and respectively $rootScope.$on in NodeController

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2015-10-16
@R0dger

read here - stepansuvorov.com/blog/2014/09/angularjs-data-transfer
I hope you will emphasize a lot for yourself.

A
Andrey Shchetinin, 2015-10-19
@draedful

The first method is better, because then the data will be located on "its own shelf".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question