0

Hey I'm trying to update an object with child properties within my angular application.

object 1:

$scope.osbStep = {
                 test0Nav : {
                     current : false,
                    complete : false,
                    hidden : false
                 },test1Nav : {
                        current : false,
                        complete : false,
                        hidden : false
                 },test2Nav : {
                        current : false,
                        complete : false,
                        hidden : false
                     },
             };

$broadcast object 2:

var currentPage = { test0Nav : { current : true }};
$rootScope.$broadcast('step:set', currentPage);

How can I update object 1 with object 2 ?

I am looping through and matching Properties. BuT I need to update object 1 with object 2 data. My logs are only returning strings.

$scope.$on('step:set', function( event, currentStepData ){

                    for ( var key in currentStepData ) {
                        if( currentStepData.hasOwnProperty( key ) ) {
                            var currentKey = key;
                            for ( var foo in $scope.osbStep ) {
                                if( $scope.osbStep.hasOwnProperty( foo ) ) {

                                    if (currentKey === foo){

                                        console.log( 'foo ', foo );
                                        console.log( 'currentKey 'currentKey );

                                    }


                                }
                            }
                        }
                    }

                });
1
  • so are you getting the right values inside your loop? Commented Jun 23, 2016 at 13:27

1 Answer 1

1

Have a look at this: http://davidcai.github.io/blog/posts/copy-vs-extend-vs-merge/

In your case, I think angular.merge is the most appropriate:

angular.merge($scope.osbStep, currentPage);

PS: If you prefer, I think you could use jQuery extend method too (should be a bit faster for big objects)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.