D
D
dimk00z2015-10-29 11:27:21
JavaScript
dimk00z, 2015-10-29 11:27:21

Don't understand how variables work in Angular?

$scope.geoObjects = [
        
            { Name: "Москва, Слесарный переулок, д.3"},
            { Name: "Люберцы, Октябрьский проспект д.143"},
            { Name: "Москва, 3-я Хорошевская улица д.2, стр.1"},
            { Name: "Москва, Нижний Сусальный переулок, д.5, стр.4"},
          
      ];
    var geocodeQuery;
    for (var i = 0, ii = $scope.geoObjects.length; i < ii; i++) {
       
       geocodeQuery = $scope.geoObjects[i].Name;
        ymaps.geocode(geocodeQuery).then(function (res) {
            res.geoObjects.each(function(geoObject){
                $scope.$apply(function(){
                 $scope.geoObjects[i]=
                 {
                 	geometry:{
                            type:'Point',
                            coordinates:geoObject.geometry.getCoordinates(),
                            properties:{
                            // А в качестве контента балуна - подробности:
                            // имена всех остальных найденных объектов.
                            balloonContent:geoObject.properties.get('name')
                        }
                        }
                 };
                });
            });
        
        });
    }

A slightly modified piece of code from the angular-yandex-map examples, I don’t understand why only the last element is updated. In the body of the ymaps.geocode(geocodeQuery) function, i is equal to the last value. Why is that?
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-10-29
@dimk00z

You do not understand variables in Angular, but scopes and asynchrony in js.
https://learn.javascript.ru/task/make-army

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question