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

查看:38
本文介绍了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 未定义

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屋!

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