【问题标题】:Is there an equivalent to $scope.$apply?有没有相当于 $scope.$apply 的?
【发布时间】:2017-02-17 10:47:02
【问题描述】:

我在 Javascript 中遇到错误,因为我的 $digest 循环显然都是同时运行的。

我得到的错误:Error: [$rootScope:inprog] $digest already in progress

我想知道我是否可以使用某些东西来停止这些循环,或者我是否没有正确定义我的功能?

这是我的控制器的代码,你可以看到有两个不同的 $scope 函数,第二个似乎是问题:

.controller('accountController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
  // Create a reference to the file we want to download
  var userId = firebase.auth().currentUser.uid;
  console.log(userId);
  var database = firebase.database().ref('/accounts/' + userId);
  var storageRef = firebase.storage();
  var pathReference = storageRef.ref('/' + userId + 'profilepic.jpg');
  // Get the download URL
  pathReference.getDownloadURL().then(function(url) {
  // Insert url into an <img> tag to "download"
    $scope.$apply(function() {
      $scope.imageUrl = url;
      database.on('value', function(snapshot) {
  var displayProfilePic = snapshot.val().photoURL;
  $scope.displayProfilePic = displayProfilePic;



  });

            });



  });
  var database = firebase.database().ref('/accounts/' + userId);
    database.on('value', function(snapshot) {
       var displayName = snapshot.val().name;
        var description = snapshot.val().description;
      var displayHobbies = snapshot.val().hobbies;
      var displayFacebook = snapshot.val().facebook;
      var displayTwitter = snapshot.val().twitter;
      var displayInstagram = snapshot.val().instagram;
      var displayYoutube = snapshot.val().youtube;
      var displaySnapchat = snapshot.val().snapchat;
      var displayLinkedin = snapshot.val().linkedin;
      var displayProfilePic = snapshot.val().photoURL;

         $scope.$apply(function() {
                        $scope.displayName = displayName;
                        $scope.description = description;
            $scope.displayHobbies = displayHobbies;
            $scope.displayFacebook = displayFacebook;
            $scope.displayTwitter = displayTwitter;
            $scope.displayInstagram = displayInstagram;
            $scope.displayYoutube = displayYoutube;
            $scope.displaySnapchat = displaySnapchat;
            $scope.displayLinkedin = displayLinkedin;
            $scope.displayProfilePic = displayProfilePic;

            // Hide Social buttons if no value in Database :
            if (displayFacebook === "") {
              document.getElementById('iconFacebook').style.color = '#EFF1F5';
              var anchor = document.getElementById("linkFacebook");  // Get the <a> element with id="myAnchor"
              var att = document.createAttribute("href");        // Create a "href" attribute
              att.value = "#";            // Set the value of the href attribute
              anchor.setAttributeNode(att);
            } else {
              document.getElementById('iconFacebook').style.color = '#80d5f2';
              var anchor = document.getElementById("linkFacebook");  // Get the <a> element with id="myAnchor"
              var att = document.createAttribute("href");        // Create a "href" attribute
              att.value = "http://www.facebook.com/" + displayFacebook;            // Set the value of the href attribute
              anchor.setAttributeNode(att);                      // Add the href attribute to <a>
              };
              if (displayTwitter === "") {
                document.getElementById('iconTwitter').style.color = '#EFF1F5';
                var anchor = document.getElementById("linkTwitter");  // Get the <a> element with id="myAnchor"
                var att = document.createAttribute("href");        // Create a "href" attribute
                att.value = "#";            // Set the value of the href attribute
                anchor.setAttributeNode(att);
              } else {
                document.getElementById('iconTwitter').style.color = '#80d5f2';
                var anchor = document.getElementById("linkTwitter");  // Get the <a> element with id="myAnchor"
                var att = document.createAttribute("href");        // Create a "href" attribute
                att.value = "http://www.twitter.com/" + displayTwitter;            // Set the value of the href attribute
                anchor.setAttributeNode(att);
                };
                if (displayInstagram === "") {
                  document.getElementById('iconInstagram').style.color = '#EFF1F5';
                  var anchor = document.getElementById("linkInstagram");  // Get the <a> element with id="myAnchor"
                  var att = document.createAttribute("href");        // Create a "href" attribute
                  att.value = "#";            // Set the value of the href attribute
                  anchor.setAttributeNode(att);
                } else {
                  document.getElementById('iconInstagram').style.color = '#80d5f2';
                  var anchor = document.getElementById("linkInstagram");  // Get the <a> element with id="myAnchor"
                  var att = document.createAttribute("href");        // Create a "href" attribute
                  att.value = "http://www.instagram.com/" + displayInstagram;            // Set the value of the href attribute
                  anchor.setAttributeNode(att);
                  };
                  if (displayYoutube === "") {
                    document.getElementById('iconYoutube').style.color = '#EFF1F5';
                    var anchor = document.getElementById("linkYoutube");  // Get the <a> element with id="myAnchor"
                    var att = document.createAttribute("href");        // Create a "href" attribute
                    att.value = "#";            // Set the value of the href attribute
                    anchor.setAttributeNode(att);
                  } else {
                    document.getElementById('iconYoutube').style.color = '#80d5f2';
                    var anchor = document.getElementById("linkYoutube");  // Get the <a> element with id="myAnchor"
                    var att = document.createAttribute("href");        // Create a "href" attribute
                    att.value = "http://www.youtube.com/" + displayYoutube;            // Set the value of the href attribute
                    anchor.setAttributeNode(att);
                    };
                    if (displaySnapchat === "") {
                      document.getElementById('iconSnapchat').style.color = '#EFF1F5';
                      var anchor = document.getElementById("linkSnapchat");  // Get the <a> element with id="myAnchor"
                      var att = document.createAttribute("href");        // Create a "href" attribute
                      att.value = "#";            // Set the value of the href attribute
                      anchor.setAttributeNode(att);
                    } else {
                      document.getElementById('iconSnapchat').style.color = '#80d5f2';
                      var anchor = document.getElementById("linkSnapchat");  // Get the <a> element with id="myAnchor"
                      var att = document.createAttribute("href");        // Create a "href" attribute
                      att.value = "http://www.snapchat.com/" + displaySnapchat;            // Set the value of the href attribute
                      anchor.setAttributeNode(att);
                      };
                      if (displayLinkedin === "") {
                        document.getElementById('iconLinkedin').style.color = '#EFF1F5';
                        var anchor = document.getElementById("linkLinkedin");  // Get the <a> element with id="myAnchor"
                        var att = document.createAttribute("href");        // Create a "href" attribute
                        att.value = "#";            // Set the value of the href attribute
                        anchor.setAttributeNode(att);
                      } else {
                        document.getElementById('iconLinkedin').style.color = '#80d5f2';
                        var anchor = document.getElementById("linkLinkedin");  // Get the <a> element with id="myAnchor"
                        var att = document.createAttribute("href");        // Create a "href" attribute
                        att.value = "http://www.linkedin.com/" + displayLinkedin;            // Set the value of the href attribute
                        anchor.setAttributeNode(att);
                        };

                });

    });

}])

感谢您的建议!

编辑:我将我的应用程序与我认为不会停止屏幕每次转换之间的循环的标签一起使用?也许?

【问题讨论】:

  • pathReference.getDownloadURL 得到解决时,你为什么要打电话给$scope.$apply
  • 亲爱的上帝,把你的代码包装在一个函数中!
  • 如果摘要已经在进行中,显然不需要以 $scope.$apply 开头。
  • 大家好,我对 Javascript 和 Ionic 还很陌生,所以我尝试使用在线教程等...我上周在 Codecademy 上学到了我能学到的东西,所以是的,我仍然有很多问题。如果您能向我展示一种更好的方法来组织我的代码和函数,我将不胜感激,这样我就不必使用 Scope one。我根本不知道任何其他方式!

标签: javascript angularjs firebase ionic-framework firebase-realtime-database


【解决方案1】:

您始终可以将$scope.$apply 替换为$scope.$evalAsync$timeout。下面解释了它们各自的作用:

$scope.$evalAsync - 在当前摘要周期内执行您的回调,或者在没有摘要运行的情况下创建一个新的回调。

$timeout - 等待当前摘要(如果有)完成,然后在新摘要中运行回调。

希望这会有所帮助。

汤姆

【讨论】:

  • 你好,是的,我都试过了,每次都出现这个错误:Uncaught ReferenceError: $timeout is not defined
  • 您必须确保将 $timeout 注入您的对象 - function($scope, $timeout) (stackoverflow.com/questions/19009370/…)
  • 是的,我知道 :) 它已全部正确输入,并且错误一直出现。我正在使用 Ionic 1,除了使用 $scope.$apply 调用时,实际上没有任何功能可以工作,没有它,您将无法在 DOM 中显示任何内容。
  • 我提供的答案毕竟应该可以工作,在幕后他们只是打电话给$scope.$apply ...就在正确的时间:-) 其他的一定是错误的。
  • 没关系,我是个白痴。我一直在写“$timeOut”,但它从来没有奏效。编辑:谢谢!
【解决方案2】:

错误:$rootScope:inprog

行动已在进行中

说明

在任何时间点都只能有一个$digest$apply 操作在进行中。这是为了防止很难检测到的错误进入您的应用程序。此错误的堆栈跟踪允许您跟踪导致错误的当前执行的$apply$digest 调用的来源。

消化阶段

AngularJS 会跟踪我们所处的处理阶段,相关的是$apply$digest尝试重新输入$digest$apply 而其中一个已经在进行中,这通常表示需要修复的编程错误。所以AngularJS会在发生这种情况时抛出这个错误。

代码显然需要修复。

诊断此错误

当您收到此错误时,诊断问题的原因可能会相当令人生畏。最好的做法是从错误中调查堆栈跟踪。您需要查找调用了$apply$digest 的位置,并找到发生这种情况的上下文。

应该有两个调用:

  • 第一个调用是好的$apply/$digest,通常会被调用堆栈顶部附近的某个事件触发。

  • 第二次调用是错误的$apply/$digest,这是需要调查的。

一旦您确定了这个调用,您就可以在堆栈中查找问题所在。

如果在您的应用程序代码中进行了第二次调用,那么您应该查看为什么从$apply/$digest 中调用了此代码。这可能是一个简单的疏忽,也可能适合前面描述的同步/异步场景。

如果第二次调用是在 AngularJS 指令中进行的,那么它很可能与前面描述的第二个编程事件触发场景相匹配。在这种情况下,您可能需要进一步向上查找触发事件的原因。

欲了解更多信息,请参阅AngularJS inprog Error Reference -- Diagnosing This Error

【讨论】:

    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2014-05-27
    • 2016-11-03
    • 1970-01-01
    • 2017-12-10
    • 2013-07-21
    相关资源
    最近更新 更多