最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send data from child controller to Parent controller in AngularJS? - Stack Overflow

programmeradmin2浏览0评论

I need some guidance to take the best practice for my task in AngularJS.

Task:

  1. Inside the view : I have one parent controller and two child controllers.
  2. Child controllers work with their own $scope and objects.
  3. When I press save in the view, I need to get the data from child controllers to parent controller in order to prepare an object for posting it to the server.

I am getting confused of what is the best solution for this approach.

I need some guidance to take the best practice for my task in AngularJS.

Task:

  1. Inside the view : I have one parent controller and two child controllers.
  2. Child controllers work with their own $scope and objects.
  3. When I press save in the view, I need to get the data from child controllers to parent controller in order to prepare an object for posting it to the server.

I am getting confused of what is the best solution for this approach.

Share Improve this question edited Feb 10, 2016 at 14:19 user1983983 4,8412 gold badges18 silver badges24 bronze badges asked Oct 18, 2015 at 19:53 Sajjad Ali KhanSajjad Ali Khan 1,8132 gold badges22 silver badges17 bronze badges 2
  • 1 could you please add some code..that would clear the picture.. – Pankaj Parkar Commented Oct 18, 2015 at 19:56
  • @Sajjad, if you found any of the answers below helpful, please accept to close the question or ask a follow up question – user2954587 Commented Oct 19, 2015 at 14:19
Add a comment  | 

4 Answers 4

Reset to default 9

A common way of sharing data between controllers is to use use a service.

You could also broadcast updates to the parent controller

There are many different ways to achieve this..

  1. Using $rootScope.
  2. Using Services
  3. Use broadcast and emit
  4. Declare an object in parent controller and then modify the same object in the child controller.

Using $rootScope is not a good approach, as the $rootScope variable is destroyed when application is closed.

I will also not recommend broadCast or emit, until it's required.

Services, is good for communication b/w controllers, but again you have inject it and modify the methods.

In your, scenario, i would recommend to use the common $scope object variable, which is declared inside parent and used in child controllers, as all methods are inherited in the child controllers.

Above answers are useful, but below is an simple way to update a $scope object in parent controller from child scope as below.

In child controller, $scope.$parent.parentObject = updatedObject

Hope it helps!

There are three common ways to share data between controllers:

1 put data in service or factory. Since they are singleton, any controller injected with same service can share the data within that service

2 put data in $rootScope

3 use $broadcast or $emit to send events between scopes, and pass the shared data as event arguments

发布评论

评论列表(0)

  1. 暂无评论