我想調用角度/ 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和文本推送爲兩個不同的對象 –