如何从在angularjs $范围平原对象吗? [英] How to get plain object from $scope in angularjs?

查看:202
本文介绍了如何从在angularjs $范围平原对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把下载的JSON对象为一个角$范围,但我发现,棱角分明加上里面的一些框架依赖属性。

I am putting the downloaded json object into an angular $scope, but I found that angular adds some framework dependent properties inside it.

我要修改的对象发送回服务器,有一个简便的方法,我可以删除角度特性,并获得普通对象,而无需像 $$ hashkey 角作用域属性?

I want to send the modified object back to server, is there a convenient way I can remove angular properties and get the plain object without angular scope properties like $$hashkey?

编辑:

可能重复的问题不提供我所需要的答案。

The possible duplicate question does not provide the answer I needed.

调用 angular.toJson 给我一个普通字符串$ SCOPE,同时呼吁 angular.copy 抛出一个错误。我猜他们没有被设计在$范围对象本身的工作?

Calling angular.toJson gives me a plain string "$SCOPE", while calling angular.copy throws an error. I guess they are not designed to work with the $scope object itself?

推荐答案

您是对的,在 angular.toJson 不支持 $范围对象,你可以在源$ C ​​$ C看到:的 Angular.js#L979

You are right, the angular.toJson doesn't support the $scope object as you can see in the source code: Angular.js#L979

您可以使用 JSON.stringify()用自定义替代功能和 angular.toJson复制逻辑的一部分()这样的;

You could use the JSON.stringify() with a custom replacer function and copy a part of the logic in angular.toJson() like this;

JSON.stringify(obj, function(key, value) {
  if (typeof key === 'string' && (key.charAt(0) === '$' || key === 'this')) {
    // ignore any key start with '$',
    // and also ignore 'this' key to avoid a circular reference issue.
    return undefined;
  }

  return value;
});

希望这有助于。

这篇关于如何从在angularjs $范围平原对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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