链接两个函数的最佳实践,AngularJs/JavaScript

Sho*_*bel 1 javascript function angularjs

我有两个功能。

单击“关闭”按钮时隐藏模态另一个功能向右滑动并将模态更改为另一种状态/视图。

当用户滑动到新状态时,我遇到了模态不“隐藏”的问题。我可以看到它有效,但模态仍然显示,我在想解决这个问题的最好方法是链接我的两个函数?这是正确的术语吗?

两个功能..

隐藏模态

$scope.closetripInfo = function() {
    $scope.modal.hide();
  };
Run Code Online (Sandbox Code Playgroud)

向右滑动以显示新的 .state

$scope.onSwipeRight = function() {
  $state.go('app.current-trip');
}
Run Code Online (Sandbox Code Playgroud)

这有效!但是.. 我想做的是将我令人兴奋的closetripInfo功能插入onSwipeRight像这样:

$scope.onSwipeRight = function() {

  $scope.closetripInfo();  
  $state.go('app.current-trip').closetripInfo();
}
Run Code Online (Sandbox Code Playgroud)

它有效,但对我来说它看起来不正确,我收到了一个错误..我没有成功地正确地做到这一点。或许没那么简单。我还认为我可能需要了解承诺之类的最佳实践?

错误

TypeError: $state.go(...).closetripInfo 不是 Scope.$scope.onSwipeRight 的函数

任何建议或资源将不胜感激。谢谢!

Mid*_*vin 5

您可以使用 AngularJS 承诺异步运行函数。AngularJS 中的 Promise 由内置$q服务提供。您可以在此处阅读。

$scope.onSwipeRight = function() {
  var deferred = $q.defer();
  deferred.resolve('res');

  $state.go('app.current-trip');

  // We return a promise
  return deferred.promise;
}


onSwipeRight().then(function(res) {
   // Success callback
   $scope.closetripInfo();
},null);
Run Code Online (Sandbox Code Playgroud)

这是有关使用该服务的小提琴$q。这也是一篇很好的博客文章,其中承诺被解释为卡通。