0

Apologies if this has been asked before, I couldn't find anything on SO and I'm hoping for some clarification ( or a nice neat trick )

Given

<div ng-controller="Parent"> 
    <div ng-controller="Child">
        //child manipulation of parent scope object
    </div>
</div>

Parent sets json object so it is available to multiple child scopes -

$scope.persistentData = getAJSONObject();

A child scope wants to do some calculations and update a key of the local json object it has inherited from the parent -

doCalculations( $scope.persistentData.keyIWantToAlter )

Do I need to explicitly assign the parent scope to the result of the calculation function in the child (shown below) or is there a way that I can propogate the changes to the parent scope by just using the child's inherited scope objects?

$parent.$scope.$persistentData.keyIWantToAlter = 
    doCalculations( $scope.persistentData.keyIWantToAlter) 
6
  • 1
    Please consider rewording, its currently really hard to follow what you are asking. Commented Mar 31, 2014 at 15:58
  • Hopefully I haven't made it worse Commented Mar 31, 2014 at 16:02
  • It is clearer.. is getAJSONObject async ? Commented Mar 31, 2014 at 16:03
  • It is async but has been provided via resolve so it should not be treated as such once the parent scope has initialized Commented Mar 31, 2014 at 16:05
  • Last ? is inside of doCalculations are you do anything like $scope.persistentData = ... Commented Mar 31, 2014 at 16:05

1 Answer 1

2

I can't see any problems with the blurb you gave you will need to give us more. I can caution you about trying to "share" nested objects on scope.

This fiddle illustrates what happens if you are in the child and you "overwrite" the reference. The json2 shows that these start off the same, but I overwrite the reference in the child scope and now the variables are detached.

I think you are experiencing a similar issue but can't prove it until you provide more info.

<div ng-controller="ParentCtrl">
  Hello, {{json2}}!
    <div ng-controller="ChildCtrl">
          Hello, {{json2}}!
    </div>
</div>



function ParentCtrl($scope) {
    $scope.json2 = {
         child:{
            name: 'parent'
        }
    }

}

function ChildCtrl($scope, $timeout) {

    $scope.json2 = {
         child:{
            name: 'child'
        }
    }

    $timeout(function(){
         $scope.json2.child.name= 'nick';
     },5000);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it was indeed a silly overwrite I hadn't spotted which leads me to think that even though the explicit $parent references are ugly they may be safer in the long run
Use services ;) you can run into all kinds of issues when you rely on nested scopes.
Thanks I must look into that, would you provide the service to the parent controller or should I be thinking entirely outside controllers ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.