Angular js:Array

时间:2018-04-04 06:15:57

标签: angularjs

我从Rest Api jsonconfig中找到了这个json:

$scope.setPolygone = function (id)
    {

        console.log(spotcam.jsonconfig);
        $scope.DrawPolygon(id, JSON.parse(spotcam.jsonconfig), true);
       }

1 个答案:

答案 0 :(得分:0)

这是我身边的逻辑实施。循环遍历数组中的每个对象。保留一个本地索引并替换数据数组中的x,y值。



 var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.data = {
        "polygon": [{
            "P1": { "x": 165, "y": 257 },
            "P2": { "x": 573, "y": 200 },
            "P3": { "x": 441, "y": 463 },
            "P4": { "x": 20,  "y": 463 }
        }]
    }
    $scope.DEFAULT_POLYGONS_POINTS = [ [0, 0], [150, 0], [150, 150], [0, 150]];
    let index = 0;
    angular.forEach($scope.data.polygon[0], function(value, key){
        value.x = $scope.DEFAULT_POLYGONS_POINTS[index][0];
        value.y = $scope.DEFAULT_POLYGONS_POINTS[index][1];
        ++index;
     });

    console.log($scope.data);
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
</div>
&#13;
&#13;
&#13;