Angular中angular.copy的替代方法是什么 [英] What's alternative to angular.copy in Angular

查看:328
本文介绍了Angular中angular.copy的替代方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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未定义

推荐答案

假设您正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆