我想调用角度/ ajax从API获取值,并希望设置我的结果期待结果格式。我试图用我的代码,但它调用的两倍,我不知道我在哪里弄错请建议使用angularjs得到结果的对象形式
$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'},
];
什么是你所得到的结果呢? –
也许试着'$ scope.results.push({'id':value.id,'text':value.name});'? –
你现在这样做的方式会将id和文本推送为两个不同的对象 –