How can I copy an object and lose its reference in Angular?
With AngularJS, I can use angular.copy(object)
, but I'm getting some error using that in Angular.
EXCEPTION: ReferenceError:
angular
is not defined
ベストアンサー1
Assuming you are using ES6, you can use var copy = Object.assign({}, original)
. Works in modern browsers; if you need to support older browsers check out this polyfill
update:
With TypeScript 2.1+, ES6 shorthand object spread notation is available:
const copy = { ...original }