Angular 中 angular.copy 的替代方法是什么 [英] What's alternative to angular.copy in Angular
问题描述
如何在 Angular 中复制对象并丢失其引用?
How can I copy an object and lose its reference in Angular?
在 AngularJS 中,我可以使用 angular.copy(object)
,但在 Angular 中使用它时出现一些错误.
With AngularJS, I can use angular.copy(object)
, but I'm getting some error using that in Angular.
例外:ReferenceError:angular
未定义
EXCEPTION: ReferenceError:
angular
is not defined
推荐答案
假设你使用的是 ES6,你可以使用 var copy = Object.assign({}, original)
.适用于现代浏览器;如果您需要支持旧浏览器,请查看此 polyfill
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
更新:
使用 TypeScript 2.1+,可以使用 ES6 速记对象扩展符号:
With TypeScript 2.1+, ES6 shorthand object spread notation is available:
const copy = { ...original }
这篇关于Angular 中 angular.copy 的替代方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!