Javascript AngularJS/Restangular索引JSON数据提取
我有一个应用程序,它接受报价输入(纯度、重量、总数),并将其推到$scope.quote:Javascript AngularJS/Restangular索引JSON数据提取,javascript,json,angularjs,restangular,Javascript,Json,Angularjs,Restangular,我有一个应用程序,它接受报价输入(纯度、重量、总数),并将其推到$scope.quote: // Controller action // $scope.quote.push({ total: ((($scope.karat * $scope.spot) * $scope.percentage) / 20) * $scope.estimatedWeight, karat: $scope.karat * 100, description: $scope.description,
// Controller action //
$scope.quote.push({
total: ((($scope.karat * $scope.spot) * $scope.percentage) / 20) * $scope.estimatedWeight,
karat: $scope.karat * 100,
description: $scope.description,
actualWeight: $scope.actualWeight,
estimatedWeight: $scope.estimatedWeight,
percent: $scope.percentage * 100,
spot: $scope.spot
})
及
以及报价完成后的发布/保存
$scope.save = function() {
var now = $scope.getDate();
$scope.quote.push({
createdOn: $scope.getDate()
})
Restangular.all('quote').post($scope.quote).then(function(quote){
$location.path('#/scrap')
});
};
当尝试访问列表或编辑的引用JSON时,由于JSON结构,我无法访问所需的所有信息
{
"0": {
"total": 401.79040000000003,
"karat": 74,
"description": "Rings",
"actualWeight": 12,
"estimatedWeight": 11,
"percent": 80,
"spot": 1234
},
"1": {
"total": 560.7296,
"karat": 56.8,
"description": "Test",
"actualWeight": 22,
"estimatedWeight": 20,
"percent": 80,
"spot": 1234
},
"2": {
"total": 48.5625,
"karat": 92.5,
"description": "Testing",
"actualWeight": 80,
"estimatedWeight": 75,
"percent": 70,
"spot": 20
},
"3": {
"createdOn": "2013-11-26T21:26:42.253Z"
},
"_id": {
"$oid": "52951213e4b05f03172f14e7"
}
}
每个索引表示报价和createdOn信息的一个行项目。我想弄清楚的是,是否有一种方法可以访问所有行项目信息,而不必调用每个单独的索引
我已经研究了一些lodash/下划线,考虑了重组后端。。。我真的不知道从这里到哪里去
由于没有太多关于后端如何工作或如何处理数据的详细信息,我假设问题在于生成JSON 我要做的是更改Angular将数据发布到服务器的方式,例如: 保存功能:
$scope.save = function() {
var url = 'http:127.0.0.1:3000/url/you/send/your/data/to',
json = JSON.stringify($scope.quote);
$http({
method: 'POST',
url: url,
data: json,
headers: {
'Content-Type': 'application/json'
}
}).then(function(response) {
$location.path('#/scrap');
});
};
这将为您提供更多的控制,并确保您可以控制如何将数据转换为正确的JSON,在本例中,使用JSON
对象的.stringify()
方法
我从你的评论中看到,你关心的是索引;通常,与服务器同步的对象具有一个属性id
,该属性表示其数据库id,而不是其客户机数组索引。这是保持客户机上的模型数据在服务器端真实性表示的一种好方法
我建议,有相当多的库可以帮助这个过程客户端,因为您已经在使用Angular-reading-In
注意:如果您想使用
JSON.stringify()
方法支持较旧的浏览器,请确保使用如何创建JSON?索引似乎不自然,如果我JSON.stringify
一个数组,它通常没有包含或更具体的索引;JSON从何而来?服务器读取了吗?我建议修复JSON,不要尝试读取它,尽管它的格式是古怪的,因为工厂正在推进导致索引的var quote=[]。但是,如果更改为{}for quote,我将使用什么方法替换$scope.quote.push(),但同时,如果失去索引,我将失去在单个quote报价中分隔行项目的能力。后端由restanglar处理。
$scope.save = function() {
var url = 'http:127.0.0.1:3000/url/you/send/your/data/to',
json = JSON.stringify($scope.quote);
$http({
method: 'POST',
url: url,
data: json,
headers: {
'Content-Type': 'application/json'
}
}).then(function(response) {
$location.path('#/scrap');
});
};