AngularJS-等待$$阶段变为空

AngularJS-等待$$阶段变为空,angularjs,Angularjs,在我的指令中,我进行API调用,获取数据并呈现它。在进行渲染时,即$digest正在运行,我希望等待,然后执行一些操作 这是我的实现,不起作用: // Assume I'm in the callback function of the $resource function(response) { // $scope.templates now needs to render $scope.templates = (_.isEmpty(response.warning)) ? res

在我的指令中,我进行API调用,获取数据并呈现它。在进行渲染时,即$digest正在运行,我希望等待,然后执行一些操作

这是我的实现,不起作用:

// Assume I'm in the callback function of the $resource
function(response) {
   // $scope.templates now needs to render
   $scope.templates = (_.isEmpty(response.warning)) ? response.data : [];

   var s = $scope.$watch('$$phase', function(v) {
       if (_.isNull(v)) {
          NowRunMyNextAwesomeAction()
          // Silent the $watch
          s();
       }
   });
}

当我将console.logv放在$watch中时,它会记录$diget,然后不会记录其他内容。为什么?我认为可能发生了其他事情,所以我尝试在1s后用$timeout记录$$phase,结果显示为null。所以它肯定没有运行

所以,我找到了一个解决方案;我不知道这是否是一个好的实践,但它依赖于使用$timeout。我的印象是不推荐在Javascript中使用计时器

无论如何,下面是如何做到这一点:

// Assume I'm in the callback function of the $resource
function(response) {
   // $scope.templates now needs to render
   $scope.templates = (_.isEmpty(response.warning)) ? response.data : [];

   $timeout(function(){
       NowRunMyNextAwesomeAction();
   });
}
请注意,$timeout的延迟为零。有关更多信息,请