From 764161054a5cbf6d2a2f8059dfa646f01e986fa2 Mon Sep 17 00:00:00 2001 From: "Joel W. Reed" Date: Fri, 29 Mar 2013 15:11:32 -0400 Subject: [PATCH] fix(copy) $resource throws TypeError when json has both properties and arrays Closes #1044 --- src/Angular.js | 2 +- test/AngularSpec.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index c07a788cf528..7bf91a3fa4ce 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -583,7 +583,7 @@ function copy(source, destination){ } } else { if (source === destination) throw Error("Can't copy equivalent objects or arrays"); - if (isArray(source)) { + if (isArray(source) && isArray(destination)) { destination.length = 0; for ( var i = 0; i < source.length; i++) { destination.push(copy(source[i])); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 7f1ab8a32c26..bb7fe4f3e5e7 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -83,6 +83,12 @@ describe('angular', function() { expect(copy([{key:null}])).toEqual([{key:null}]); }); + it("should copy an object with properities and arrays", function() { + var src = {name:"value", "array1": [ {"p1": "v1"}, {"p2": "v2"} ]}; + var dst = copy(src); + expect(dst).toEqual(src); + }); + it('should throw an exception if a Scope is being copied', inject(function($rootScope) { expect(function() { copy($rootScope.$new()); }).toThrow("Can't copy Window or Scope"); }));