Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  163] [ 1]  / answers: 1 / hits: 83499  / 11 Years ago, tue, june 4, 2013, 12:00:00

I have two controllers, one wrapped within another. Now I know the child scope inherits properties from the parent scope but is there a way to update the parent scope variable? So far I have not come across any obvious solutions.



In my situation I have a calendar controller within a form. I would like to update the start and end dates from the parent scope (which is the form) so that the form has the start and end dates when submitted.


More From » angularjs

 Answers
6

You need to use an object (not a primitive) in the parent scope and then you will be able to update it directly from the child scope



Parent:



app.controller('ctrlParent',function($scope){
$scope.parentprimitive = someprimitive;
$scope.parentobj = {};
$scope.parentobj.parentproperty = someproperty;
});


Child:



app.controller('ctrlChild',function($scope){
$scope.parentprimitive = this will NOT modify the parent; //new child scope variable
$scope.parentobj.parentproperty = this WILL modify the parent;
});


Working demo: http://jsfiddle.net/sh0ber/xxNxj/



See What are the nuances of scope prototypal / prototypical inheritance in AngularJS?


[#77815] Monday, June 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;