2016-09-30 68 views
2

即時通訊使用angularjs框架。 這是我第一次代碼:帶有對象的AngularJS數組

var app = angular.module('app',['ngRoute','ngAnimate']); 

app.controller('maincontroller',function($scope,$http) { 
    $scope.planes = []; 

    $scope.createPlanes = function() { 
     $http.post('api/create_plane.php',{ 
     }).success(function(data,status,headers,config) { 
      console.log(data); 
      for(var result in data) 
       { 
        $scope.planes = [ 
         {"planeLMID": data[result]['planeLMID']} 
        ]; 
       } 
      console.log($scope.planes); 
     }); 
    }; 
    $scope.createPlanes(); 
}); 

這是執行console.log(數據)的回報;

Object {1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 6: Object, 7: Object, 8: Object, 9: Object, 14: Object, 15: Object, 16: Object, 17: Object, 18: Object, 20: Object, 21: Object, 23: Object, 24: Object, 25: Object, 26: Object, 27: Object, 29: Object, 31: Object, 32: Object, 33: Object, 34: Object, 35: Object, 36: Object, 37: Object, 38: Object} 

而且在它的對象看怎麼樣

planeAFID: "89-3412" 
planeCPISPI: "" 
planeLMID:  "8215" 

一個樣品,所以我想做一個$ scope.planes陣列for循環。 但第一個對象後,它已經填補了。我如何將其他對象推入數組?

感謝

+1

你可以做'$ scope.planes.push({ 「planeLMID」:數據[結果] [ 'planeLMID']})' –

+0

上帝啊,對不起,小離我失敗。 無論如何,謝謝 – Kavkus

回答

0

使用數組的push功能可以實現此功能。

$scope.planes=[]; 
for(var result in data) 
{ 
    $scope.planes.push({"planeLMID": data[result]['planeLMID']}); 

} 
console.log($scope.planes); 
1

您需要實現方法,$ scope.planes.push( '插入你想在這裏推什麼')

0

您應該使用數組的.push()方法

var app = angular.module('app',['ngRoute','ngAnimate']); 

app.controller('maincontroller',function($scope,$http) { 
    $scope.planes = []; 

    $scope.createPlanes = function() { 
     $http.post('api/create_plane.php',{ 
     }).success(function(data,status,headers,config) { 
      console.log(data); 
      $scope.planes = []; 
      for(var result in data) 
      { 
       $scope.planes.push({"planeLMID": data[result]['planeLMID']}) 
      } 
      console.log($scope.planes); 
     }); 
    }; 
    $scope.createPlanes(); 
}); 
0

你可以使用類似這樣的東西。

$scope.planes = []; 

$scope.createPlanes = function() { 
    $http.post('api/create_plane.php',{ 
    }).success(function(data,status,headers,config) { 
     console.log(data); 
     for(var result in data) 
      { 
       $scope.planes.push({ 
        "planeLMID": data[result]['planeLMID'] 
       }); 
      } 
     console.log($scope.planes); 
    }); 
}; 
$scope.createPlanes();