我不明白为什么要更改副本的值,更改$scope的值:
var tmpmember = $scope.registration.member;
console.log($scope.registration.member.birth);
tmpmember.birth=$filter('date')($scope.registration.member.birth,'yyyy-MM-dd');
console.log(tmpmember.birth);
console.log($scope.registration.member.birth);产出:
Thu Mar 11 1954 01:00:00 GMT+0100 (CET)
261 1954-03-11
262 1954-03-11有人能给我解释一下吗?
非常感谢
发布于 2015-10-08 11:10:30
在您提供的代码中,您还没有复制该对象。相反,您已经创建了指向同一个对象实例的第二个变量。
如果你真的想要一个副本,而不是一个额外的引用,angular.copy,你可以使用一个函数。https://docs.angularjs.org/api/ng/function/angular.copy#!/
var tmpmember = angular.copy($scope.registration.member);
console.log($scope.registration.member.birth);
tmpmember.birth = $filter('date')($scope.registration.member.birth, 'yyyy-MM-dd');
console.log(tmpmember.birth);
console.log($scope.registration.member.birth);结果:
Thu Mar 11 1954 01:00:00 GMT+0100 (CET)
261 1954-03-11
Thu Mar 11 1954 01:00:00 GMT+0100 (CET)https://stackoverflow.com/questions/33013917
复制相似问题