Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Date property cannot be merged through "angular.merge" #11974

Closed
shaunxu opened this issue May 29, 2015 · 2 comments
Closed

Date property cannot be merged through "angular.merge" #11974

shaunxu opened this issue May 29, 2015 · 2 comments

Comments

@shaunxu
Copy link

shaunxu commented May 29, 2015

Assuming I have the code.

$scope.obj1 = {
  name: 'Shaun',
  birthDate: new Date(1979, 5, 27)
};
$scope.obj2 = {
  name: 'Shaun Xu'
};
$scope.result = angular.merge({}, obj1, obj2);

I think $scope.result should be

{
  name: 'Shaun Xu',
  birthDate: new Date(1979, 5, 27)
}

But I got actually

{
  name: 'Shaun Xu',
  birthDate: {}
}

I reviewed the source code and found this is because, when performed baseExtend it will create an empty object property to destination when source property is object, this is case birthDate is Date and also Object. But later when perform baseExtend inside birthDate there will be nothing to copy since Date object doesn't have any enumerable member.

I think you should modify baseExtend, if the source property is Date, just copy the value to destination rather than performing deep copy.

function baseExtend(dst, objs, deep) {
  ... ...
  if (deep && isObject(src) && !angular.isDate(src)) {
    if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
    baseExtend(dst[key], [src], true);
  } else {
    dst[key] = src;
  }
}
@gkalpak
Copy link
Member

gkalpak commented May 29, 2015

#11720 will address this issue.

@gabrielmaldi
Copy link
Contributor

We should close this now that #11720 is merged.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants