我正在尝试调用angular / ajax从API获取值,并希望设置我的结果以期望结果格式。我尝试使用我的代码,但它的调用次数是两次,我不知道我错在哪里,请建议
$http.get('v1/res')
.success(function (data) {
$scope.results = [];
angular.forEach(data.data, function(value, key){
$scope.results.push({id: value.id});
$scope.results.push({text: value.name});
});
});
//期待结果
$scope.results = [
{id: 1, text: 'A'},
{id: 2, text: 'B'},
{id: 3, text: 'C'},
{id: 4, text: 'D'},
{id: 5, text: 'E'},
{id: 6, text: 'F'},
{id: 7, text: 'G'},
{id: 8, text: 'H'},
];
答案 0 :(得分:1)
如果您从api调用获得的结果与您想要的对象相同,那么您可以按照以下方式执行:
public void into(@NonNull Target target) {
long started = System.nanoTime();
checkMain();
...
}
,否则
//assuming that data.data is an array of objects that you wish for
$scope.result = []
angular.copy(data.data, $scope.result)
答案 1 :(得分:0)
从data.data
获取所需数据是不够的,因为它是具有您想要的值的对象。
如果您希望获得期望的格式,object
id
和text
的属性应存储在$scope.results
中,但在您的代码中,它们正在存在分别存储为具有一个属性的单个对象。所以你的代码应该像这样改变:
angular.forEach(data.data, function(value, key){
$scope.results.push({id: value.id, text: value.name});
});
{id: value.id, text: value.name}
是您希望存储在$scope.results
中的对象。