如何在angularjs中迭代$ scope变量?

时间:2016-01-10 12:27:44

标签: javascript arrays angularjs

我有这样的代码:

$scope.maps = [
    new google.maps.LatLng($scope.longitude[0], $scope.latitude[0]),
    new google.maps.LatLng($scope.longitude[1], $scope.latitude[1]),
    new google.maps.LatLng($scope.longitude[2], $scope.latitude[2])];

如何简化AngularJS样式以进行更多迭代?

2 个答案:

答案 0 :(得分:2)



$scope.maps = [];
$scope.longitude.forEach(function(el, i) {
  $scope.maps.push(new google.maps.LatLng(el, $scope.latitude[i]));
});

// or

$scope.maps = [];
for(var i = 0; i < $scope.longitude.length; ++i) {
  $scope.maps.push(new google.maps.LatLng($scope.longitude[i], $scope.latitude[i]));
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只是为了澄清CodeiSir的答案。 不是&#34; Angular&#34;迭代他展示的是javascript。您也可以将它用于读取数据,因为您在html中使用了角度ng-repeat。

more info here