How to send data from child controller to Parent controller in AngularJS?
A common way of sharing data between controllers is to use use a service.
You could also broadcast updates to the parent controller
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 many different ways to achieve this..
- Using $rootScope.
- Using Services
- Use broadcast and emit
- 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.