[英]How to get plain object from $scope in angularjs?
I am putting the downloaded json object into an angular $scope, but I found that angular adds some framework dependent properties inside it. 我将下载的json对象放入一个有角度的$ scope中,但我发现angular在其中添加了一些与框架相关的属性。
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
? 我想将修改后的对象发送回服务器,是否有一种方便的方法可以删除角度属性并获取没有角度范围属性的普通对象,如
$$hashkey
?
EDIT: 编辑:
The possible duplicate question does not provide the answer I needed. 可能重复的问题没有提供我需要的答案。
Calling angular.toJson
gives me a plain string "$SCOPE"
, while calling angular.copy
throws an error. 调用
angular.toJson
会给我一个简单的字符串"$SCOPE"
,而调用angular.copy
抛出错误。 I guess they are not designed to work with the $scope object itself? 我猜他们不是设计用于$ scope对象本身?
You are right, the angular.toJson
doesn't support the $scope
object as you can see in the source code: Angular.js#L979 你是对的,
angular.toJson
不支持$scope
对象,你可以在源代码中看到: Angular.js#L979
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()
与自定义的replacer函数一起使用,并像这样复制angular.toJson()
的一部分逻辑;
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;
});
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.