【问题标题】:how to add $q to order the execution?如何添加 $q 来命令执行?
【发布时间】:2017-04-26 07:41:06
【问题描述】:

我需要下令执行。我需要确保我的 COMPLETE coords() 方法在被调用后完成。如何向它添加一个承诺或 $q ?我尝试在控制台上打印,发现 coords 中的 forEach 循环在执行 coords() 函数的最后一行后完成。请参阅下面的代码。我对角度帮助真的很陌生

var appa = angular.module('appa',['firebase','uiGmapgoogle-maps']);
appa.controller('mainCtrl', function($firebaseObject,$scope,$q) {

              $scope.coords = function(){
               
                var ref =  firebase.database().ref();
                var latArray = [];
                var lngArray = [];
                var cenlat;
                var cenlng;
                  var marker = [];
                ref.once("value")
                .then(function(snapshot)
                {
                snapshot.forEach(function(child)
                {

                  latArray.push(child.child("Lat").val());
                  console.log(child.child("Lat").val());
                  lngArray.push(child.child("Long").val());
                  var mark = {
                  id: child.child("Id").val(),
                  coords: {
                  latitude: child.child("Lat").val(),
                  longitude: child.child("Long").val()
                  },
                  options: { title: child.child("Alt").val() }
                  };
                  marker.push(mark);
CONSOLE.LOG("wWHY IS THIS PRINTED aFTER??? AND HOW TO HANDLE THIS ??");
                });

                   cenlat = (Math.max.apply(Math,latArray)+Math.min.apply(Math,latArray)/2);
                   cenlng = (Math.max.apply(Math,lngArray)+Math.min.apply(Math,lngArray)/2);

                });
                $scope.map.center.latitude = cenlat;
                $scope.map.center.longitude = cenlng;
CONSOLE.LOG("wWHY IS THIS PRINTED BEFORE??? AND HOW TO HANDLE THIS ??");
});
                };

              $scope.map = {
                center:
                {
                  latitude: 51,
                  longitude: 4
                         },
                 zoom: 2
                      };

      $scope.coords();
      


   });

【问题讨论】:

  • 你注意到sn-p中的语法错误了吗?您发布的代码是无效的 javascript,您真的应该修复它并发布具有正确格式的代码,就目前而言,您的代码更难阅读,这肯定不是您编写代码的方式?!?而且 CONSOLE.LOG 无论如何都会失败,它是 console.log - javascript 不是 Visual Basic,它区分大小写
  • nemoAsad,您已经通过收集证据完成了 95% 的调试 - 两条消息的记录顺序错误 - 所以您知道代码执行的时间。您需要做的就是改变代码,以便$scope.map 语句在正确的时间执行。这并不复杂。
  • 一旦修复,还有更多.... (1) 您的平均计算似乎是错误的;你很可能想要(max + min)/2,而不是(max + min/2)。 (2) 记住 lat 和 lng 的范围是 -180 到 +180。 (max + min)/2 适用于任何经纬度群和跨越格伦威治子午线的 lng,但对于跨越国际日期变更线的 lng,(max + min)/2 会给出意想不到的结果(并且说“它永远不会发生”还不够好,因为总有一天会的!)。 (3) 最大值和最小值的简单算术平均值有利于异常值;请考虑使用“重心”算法。

标签: javascript angularjs promise q


【解决方案1】:

您正在使用 firebase 并且可以轻松地检查返回的子节点,并将它们存储在一个变量中。然后在 forEach 循环中递减变量。

//initialized var a, is having child count
a = snapshot.numChildren();

下面的完整代码将创建有序执行,并有一个基本的 Promise 实现。我还添加了 console.log 来显示订单。希望对您有所帮助。

//complete code

var appa = angular.module('appa',['firebase','uiGmapgoogle-maps']);
appa.controller('mainCtrl', function($firebaseObject,$scope,$q){

              $scope.coords = function(){
var q = $q.defer();
                var ref =  firebase.database().ref();
                var latArray = [];
                var lngArray = [];
                var cenlat;
                var cenlng;
                var a="0";
                var marker = [];

                ref.once("value")
                .then(function(snapshot)
                {
               a = snapshot.numChildren();
                console.log(a);
                snapshot.forEach(function(child)
                {
                  latArray.push(child.child("Lat").val());
                  console.log(child.child("Lat").val());
                  lngArray.push(child.child("Long").val());
                  var mark = {
                  id: child.child("Id").val(),
                  coords: {
                  latitude: child.child("Lat").val(),
                  longitude: child.child("Long").val()
                  },
                  options: { title: child.child("Alt").val() }
                  };
                  a= a-1;
                  console.log("this is current"+a);
                  marker.push(mark);
                });
console.log("hi"+a);
console.log("going for cenlat"+cenlat);
            cenlat = (Math.max.apply(Math,latArray)+Math.min.apply(Math,latArray)/2);
console.log("done with cenlat"+cenlat);
console.log("going for cenlng"+cenlng);
            cenlng = (Math.max.apply(Math,lngArray)+Math.min.apply(Math,lngArray)/2);
console.log("done with cenlng"+cenlng);
            $scope.map.center.latitude = cenlat;
            $scope.map.center.longitude = cenlng;
            if(a==0){
            q.resolve('resolved');
            }
            else{
            q.reject('rejected');
            }
                });

                };
              $scope.map = {
                center:
                {
                  latitude: 51,
                  longitude: 4
                         },
                 zoom: 2
                      };

    $scope.coords().then(
        function(v){
console.log(v);
      },
    function(err){
console.log(err);
    });

   });

【讨论】:

  • 请至少修复缩进!
  • 我知道 OP 要求与 $q 有关,但这不是必需的。我猜上面的 if(a == 0) (...) 块应该展示一些东西,但是 what 到底是什么!?在目前的形式下,它什么也不做,什么也不做,只是给下一笔电费增加一点。
猜你喜欢
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 2020-05-18
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
相关资源
最近更新 更多