我有一个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"
}}
]
任何想法或建议将不胜感激。
答案 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);
};
JSON输出
{
"john's list": [
{
"id": 3,
"name": "Coconuts",
"Amount": 10,
"Price": 10
},
{
"id": 2,
"name": "Peaches",
"Amount": 5,
"Price": 5
}
]
}
这是否符合您的预期输出?