AngularJs: Copying JSON objects

We all might be facing issues while making a copy of json object in angularjs. Mostly the copied object also changes when the original object is changed, this is because original & the copied both point to the same reference. There are different ways to overcome the issue, I’m using serialize and de-serialize of json object which will remove the hash reference.

var jsonString = angular.toJson(sourceObject) – returns the string format of json object

var jsonObj = angualr.fromJson(jsonString) – returns json object from json string

This way we can avoid copying of objects by reference.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s