1

I'm trying to pass a merged object to the getData() function:

     $scope.query = {
        order: 'date',
        limit: 15,
        page: 1
     };
     $scope.src = {txt:0};
     $scope.onReorder = function (order) {
       var toBeExtended = angular.copy($scope.src);
       var extendedObj = angular.merge({}, $scope.query,{where:toBeExtended},{order: order});
       getData(extendedObj)
     };

I don't understand why I get different results if I console.log() the variable assigned to the merge or the merge itself. In other words:

 console.log(extendedObj); shows: 
                                 Object
                                   limit: 15
                                   order: "date"
                                   page: 1
                                   where: Object
                                          __proto__: Object
                                   __proto__: Object

  console.log(angular.merge({},$scope.query,{where:toBeExtended},{order:order})); shows:                       
                                    Object
                                     limit: 15
                                     order: "date"
                                     page: 1
                                     where: Object
                                            src: 0
                                          __proto__: Object
                                     __proto__: Object

And of course I'm unable to pass the complete object.

1 Answer 1

1

Current angular.merge method consider 3rd parameter which goes for merging process. In this case {where:toBeExtended} is passed for merging process & {order: order} is ignored.

You need to first create whole object to be merge an then pass that object to merge method.

var objectToBeMerge = {};
angular.extend(objectToBeMerge, {where:toBeExtended})
angular.extend(objectToBeMerge, {order: order})
var extendedObj = angular.merge({}, $scope.query, objectToBeMerge);
Sign up to request clarification or add additional context in comments.

3 Comments

@Cris69 could you provide me plunkr? that would be best then.
I made a plunker here, your suggestion is correct. For some reasons this is not working on my code. Thx anyway.
Do replicate the same issue by taking object which you are using in your case.. You could get that clue..otherwise i will help you out with that.. Do upvote if it does hepled. Thanks ;)

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.