angularjs - 以 Angular 2 创建深拷贝

标签 angularjs angular

<分区>

我如何在 Angular 2 中创建深层复制,我尝试使用 let newObject = Object.assign({}, myObject)myObject 仍然反射(reflect)了所有更改在 newObject 中完成。

最佳答案

只需使用以下函数:

/**
 * Returns a deep copy of the object
 */

public deepCopy(oldObj: any) :any {
    var newObj = oldObj;
    if (oldObj && typeof oldObj === "object") {
        newObj = Object.prototype.toString.call(oldObj) === "[object Array]" ? [] : {};
        for (var i in oldObj)
            newObj[i] = this.deepCopy(oldObj[i]);
    }
    return newObj;
}

关于angularjs - 以 Angular 2 创建深拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966926/

相关文章:

javascript - 未捕获( promise 中): TypeError: data. 数据不可迭代

javascript - webpack 前端和后端都可以工作吗?

angular - 在 tsconfig.json 中包含多个 node_modules

javascript - 访问 jade 循环内的 Controller 变量

javascript - 使用 AngularJS 1.6.1、Node.js 和 Express.js 出现错误 : Module 'myApp' is not available!

创建泛型方法的 Angular 最佳实践

Angular2 Oninit() 如何指定要监视的数据绑定(bind)属性?

javascript - 在 ionic/angularJS 应用程序中的选项卡之间切换时,Google map 显示为空白

javascript - 如何使用另一个 ng-option 选定值动态更新 ng-option 值?

CSS/AngularJS,AngularJS Material 按钮上的文本方向