Barry
Barry

Reputation: 1820

Angular merge not working

Using Angular 1.5.8.

I have a function other components call to keep the parent updated. The parent maintains the master copy of the entity being edited which I will call widget. On init I set three fields on the widget

{a:Array[0],b: {},c:{}} 

Once one of the components loads data via $resource it calls into the update function with an Object that tries to set d. When I print d in dev tools console it says d is an object an when I expand it I get:

status: Resource
    x: "something"
    y: "something else"

next I call angular.toJson on d and get {"status":{"x":"something","y":"something else"}}

finally I call angular.merge(widget,resultFromToJsonAbove) and print the result I get back my original widget with no status object. When I experiment in a plunker with 1.5.8 and minimal example status gets added to widget. Am I missing anything or overlooking something?

Upvotes: 0

Views: 380

Answers (1)

szmeti
szmeti

Reputation: 266

angular.toJson creates a string and you cannot merge a string onto an existing object, because angular.merge expects two objects (maps). You are calling it with an object and a string.

If you really want to do it this way, you will need to deserialize the string back to an object with angular.fromJson and pass that to merge.

Upvotes: 2

Related Questions