如何在angularjs中迭代$scope变量

How to iteration on $scope variable in angularjs?

本文关键字:scope 变量 迭代 angularjs      更新时间:2023-09-26

我有这样的代码:

$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风格中进行简化以进行更多迭代?

$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]));
}

只是为了澄清CodeiSir的答案。不是"Angular"迭代,他展示的是javascript。您也可以使用它来读取数据,就像您在html中使用angular ng repeat一样。

更多信息请点击此处