简体   繁体   English

Angular.js链接然后相互依赖

[英]Angularjs chaining .then to depend on each other

I have a chain of events set in .then statements. 我在.then语句中设置了一系列事件。 I wish I understood a way to use .when() in this case. 我希望我了解在这种情况下使用.when()的方法。 This function is called on a ngn-click. 在ngn-click上调用此函数。 The issue is that I have to click twice for it to go through. 问题是我必须单击两次才能通过。 The $rootScope.csa has data going into the function that is used in the .then( functions ). $ rootScope.csa的数据将进入.then(functions)中使用的函数。 I when in inspect in the chrome debugger the and step through everything works fine I believe it is because the debugger is slowing down the application and allowing it to keep up with its self. 我在chrome调试器中进行检查并逐步运行正常时,我相信这是因为调试器正在减慢应用程序的运行速度,并使其与自身保持同步。 Other wise when I go through with out the debugger it goes so fast that it takes two clicks for $rootScope.csa.section.data to be populated for the next function to work as expected. 否则,当我用完调试器时,它运行得如此之快,以至于需要两次单击才能填充$ rootScope.csa.section.data,以使下一个功能正常工作。 The first two then statement functions are services that are wrapped in a promise and $timeout on there end and the $timeouts do not seem to be delaying the process. 然后,头两个then语句函数是包装在promise和$ timeout中的服务,并且$ timeouts似乎并没有延迟该过程。 I have looked over q.derer() many times but cannot wrap my head around how it would be implemented in this case. 我已经看过q.derer()了很多次,但无法确定在这种情况下如何实现。 Any help or information to get to the needs that I am looking for would ne appreciated. 任何帮助或信息,以满足我正在寻找的需求将不胜感激。

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')
});

You always need to pass a callback function to then , not some call result (even if it is a promise). 您始终需要将回调函数传递给then ,而不是某些调用结果(即使它是一个promise)。 I have not wrapped my head around what these functions do, but try 我还没有完全了解这些功能的作用,但是请尝试

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')
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM