如何使用输入命名数组

时间:2014-09-28 05:55:36

标签: arrays angularjs

我有一个Grocery List,我可以从一系列食物中创建。我想要做的是在存储时命名数组。我的副本挂在这里Plunker

目前输出看起来像

    [
  {
    "id": 3,
    "name": "Coconuts"
  },
  {
    "id": 2,
    "name": "Peaches"
  },
  {
    "id": 1,
    "name": "Oranges"
  }
]

我希望它像

    [
  {"John's List":
  {
    "id": 3,
    "name": "Coconuts"
  },
  {
    "id": 2,
    "name": "Peaches"
  },
  {
    "id": 1,
    "name": "Oranges"
  }}
]

任何想法或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

更新了您的保存功能,如下所示

            $scope.save = function () {
              var entity = {};
              entity[$scope.name] = $scope.NewList;
                $scope.MyList = angular.copy(entity);
            };

来自

           $scope.save = function () {
                $scope.MyList = angular.copy($scope.NewList);
            };

结帐update plunker

JSON输出

 {
  "john's list": [
    {
      "id": 3,
      "name": "Coconuts",
      "Amount": 10,
      "Price": 10
    },
    {
      "id": 2,
      "name": "Peaches",
      "Amount": 5,
      "Price": 5
    }
  ]
}

这是否符合您的预期输出?