concat two json objects using angularjs

1,208 views
Skip to first unread message

Apekshith Ramesha

unread,
Nov 19, 2013, 3:03:54 PM11/19/13
to ang...@googlegroups.com
Hello I'm trying to concat two JSON object to form a bigger json body. both these objects are form values which are assigned to a rootscope variable.

$rootScope.AString = angular.toJson(AModel);
        $rootScope.BString = angular.toJson(BModel);
        $rootScope.CString = BString.concat(AString);

But this does not work! I searched through all the documentation. there is no clear function to do this. please help!

Sander Elias

unread,
Nov 20, 2013, 1:42:19 AM11/20/13
to ang...@googlegroups.com
$rootscope.CString = angular.toJson(angular.extend(Amodel, Bmodel))

Rajesh Segu

unread,
Nov 20, 2013, 2:40:33 PM11/20/13
to ang...@googlegroups.com
One thing to note is that, angular.extend does not doe a deep extend, instead only one level. Which means, there could be possibility that the resultant object could be lossy. Just search online for deep copy 

Just a simple example:
_.deepObjectExtend(target, source) {
    for (var prop in source)
        if (prop in target)
            _.deepObjectExtend(target[prop], source[prop]);
        else
            target[prop] = source[prop];
    return target;
}

Hope this helps.

- Rajesh Segu

On Tuesday, November 19, 2013 10:42:19 PM UTC-8, Sander Elias wrote:
$rootscope.CString = angular.toJson(angular.extend(Amodel, Bmodel))

Reply all
Reply to author
Forward
0 new messages