简体   繁体   English

带有对象的 AngularJS 数组

[英]AngularJS array with Objects

im working with the angularjs framework.我正在使用 angularjs 框架。 Here is my Code first:这是我的代码:

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();
});

And this is the return of the console.log(data);这是console.log(data)的返回;

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}

And a sample of how a object in it look like以及其中的对象外观示例

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

so i want to do a $scope.planes array with a for loop.所以我想用 for 循环做一个 $scope.planes 数组。 But after the first object it doenst fill anymore.但是在第一个对象之后它不再填充。 How can i push the other objects into the array?如何将其他对象推入数组?

Thanks谢谢

你需要实现push方法, $scope.planes.push('insert what you want to push here')

You should use the array's .push() method你应该使用数组的.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();
});

you can use something like this.你可以使用这样的东西。

$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();

using push function of the array you can achive this.使用数组的推送功能,您可以实现这一点。

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

}
console.log($scope.planes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM