【问题标题】:Angularjs chaining .then to depend on each otherAngularjs 链接 .then 以相互依赖
【发布时间】:2014-03-07 01:03:04
【问题描述】:

我在 .then 语句中设置了一系列事件。我希望我能理解在这种情况下使用 .when() 的方法。这个函数在一次 ngn-click 上被调用。问题是我必须单击两次才能通过。 $rootScope.csa 有数据进入 .then(functions) 中使用的函数。我在 chrome 调试器中检查并逐步检查一切工作正常,我相信这是因为调试器正在减慢应用程序的速度并允许它跟上自己的步伐。否则,当我在没有调试器的情况下完成时,它运行得如此之快,以至于需要两次单击才能填充 $rootScope.csa.section.data 以使下一个函数按预期工作。前两个 then 语句函数是包装在 promise 和 $timeout 中的服务,并且 $timeouts 似乎并没有延迟该过程。我已经看过 q.derer() 很多次,但无法理解在这种情况下如何实现它。任何帮助或信息来满足我正在寻找的需求都将不胜感激。

audit.LockData('section', $scope.csa.ID, user.ID, $scope.csa.Revision)
    .then($rootScope.csa = audit.RecordsCheck($rootScope.csa))  //  gets data and pupulates it to the $rootscope.csa.section.data
    .then($rootScope.csa = audit.GetInstance($rootScope.csa), function(){ return $rootScope.csa})  //  gets ID(s) from $rootScope.csa.section.data.instances.ID(s) and populates them to the $rootScope.csa.section.instances
    .then(function() {if($rootScope.csa.section.data) $location.path('/Update')})
    .then(function() {if($rootScope.csa.section.data) $rootScope.$broadcast('root_updated')
});

【问题讨论】:

    标签: javascript angularjs promise deferred


    【解决方案1】:

    您总是需要将回调函数传递给then,而不是一些调用结果(即使它是一个承诺)。我还没有完全理解这些函数的作用,但是尝试一下

    audit.LockData('section', $scope.csa.ID, user.ID, $scope.csa.Revision)
    .then(function(lockResult) {
        return audit.RecordsCheck($rootScope.csa)) // gets data and pupulates it to the $rootscope.csa.section.data
    })
    .then(function(checkResult) {
        return audit.GetInstance($rootScope.csa) //  gets ID(s) from $rootScope.csa.section.data.instances.ID(s) and populates them to the $rootScope.csa.section.instances
    })
    .then(function(instance) {
        if ($rootScope.csa.section.data) {
            $location.path('/Update')
            $rootScope.$broadcast('root_updated')
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多