添加到我的 JSON.stringify 结果中的 $$hashKey 是什么 [英] What is the $$hashKey added to my JSON.stringify result

查看:28
本文介绍了添加到我的 JSON.stringify 结果中的 $$hashKey 是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试查看 MozillaJSON stringify 他们的文档以及这里在 SO 和 Google 上的页面,但没有找到任何解释.我已经多次使用 JSON.stringify 但从未遇到过这个结果.

我有一组 JSON 对象:

<预><代码>[{param_2":描述 1",param_0":名称 1",param_1":版本 1"},{param_2":描述 2",param_0":名称 2",param_1":版本 2"},{param_2":描述 3",param_0":名称 3",param_1":版本 3"}]

它附加到我的$scope.为了将它们 POST 作为一个参数,我使用了 JSON.stringify() 方法并收到以下内容:

<预><代码> [{param_2":描述 1",param_0":名称 1",param_1":版本 1",$$hashKey":005"},{param_2":描述 2",param_0":名称 2",param_1":版本 2",$$hashKey":006"},{param_2":描述 3",param_0":名称 3",param_1":版本 3",$$hashKey":007"}]

我只是好奇 $$hashkey 属性到底是什么,因为我期望 stringify 方法中的内容更类似于以下内容(也就是说,没有$$hashkey):

<预><代码>[{1":{param_2":描述 1",param_0":名称 1",param_1":版本 1"},2":{param_2":描述 2",param_0":名称 2",param_1":版本 2"},3":{param_2":描述 3",param_0":名称 3",param_1":版本 3"}}]

我不确定这是否是一个因素,但我正在使用以下内容:

  • Angularjs 1.1.5,
  • JQuery 1.8.2
  • Spring 3.0.4

我也在服务器端使用 Spring security 3.0.7.

它没有给我造成任何问题,但我想知道 $$hashkey

的原因和原因

解决方案

Angular 添加了这个来跟踪你的变化,所以它知道什么时候需要更新 DOM.

如果你使用 angular.toJson(obj) 而不是 JSON.stringify(obj) 那么 Angular 会为你去掉这些内部使用的值.

此外,如果您将重复表达式更改为使用 track by {uniqueProperty} 后缀,Angular 根本不必添加 $$hashKey.例如

    <li ng-repeat="link in navLinks track by link.href"><a ng-href="link.href">{{link.title}}</a>

永远记住您需要链接".表达的一部分 - 我总是倾向于忘记这一点.只是track by href肯定不行.

I have tried looking on the Mozilla JSON stringify page of their docs as well as here on SO and Google but found no explanation. I have used JSON.stringify many time but never come across this result.

I have an array of JSON objects:

[
    {
        "param_2": "Description 1",
        "param_0": "Name 1",
        "param_1": "VERSION 1"
    },
    {
        "param_2": "Description 2",
        "param_0": "Name 2",
        "param_1": "VERSION 2"
    },
    {
        "param_2": "Description 3",
        "param_0": "Name 3",
        "param_1": "VERSION 3"
    }
]

It is attached to my $scope. In order to POST them as one parameter, I used the JSON.stringify() method and receive the following:

   [
        {
            "param_2": "Description 1",
            "param_0": "Name 1",
            "param_1": "VERSION 1",
            "$$hashKey": "005"
        },
        {
            "param_2": "Description 2",
            "param_0": "Name 2",
            "param_1": "VERSION 2",
            "$$hashKey": "006"
        },
        {
            "param_2": "Description 3",
            "param_0": "Name 3",
            "param_1": "VERSION 3",
            "$$hashKey": "007"
        }
    ]

I am just curious about what the $$hashkey property is exactly, as I expected something more similar to the following from the stringify method (that is, without the $$hashkey):

[
    {
        "1":{
            "param_2": "Description 1",
            "param_0": "Name 1",
            "param_1": "VERSION 1"
        },
         "2":{
            "param_2": "Description 2",
            "param_0": "Name 2",
            "param_1": "VERSION 2"
        },
         "3":{
            "param_2": "Description 3",
            "param_0": "Name 3",
            "param_1": "VERSION 3"
        }
    }
]

I am not sure if it is a factor, but I am using the following:

  • Angularjs 1.1.5,
  • JQuery 1.8.2
  • Spring 3.0.4

I'm also using Spring security 3.0.7 on the Server side.

It is not causing me any issues, but I would like to know the cause and reason for the $$hashkey

解决方案

Angular adds this to keep track of your changes, so it knows when it needs to update the DOM.

If you use angular.toJson(obj) instead of JSON.stringify(obj) then Angular will strip out these internal-use values for you.

Also, if you change your repeat expression to use the track by {uniqueProperty} suffix, Angular won't have to add $$hashKey at all. For example

<ul>
    <li ng-repeat="link in navLinks track by link.href">
        <a ng-href="link.href">{{link.title}}</a>
    </li>
</ul>

Just always remember you need the "link." part of the expression - I always tend to forget that. Just track by href will surely not work.

这篇关于添加到我的 JSON.stringify 结果中的 $$hashKey 是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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