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

时间:2021-12-20 08:28:37

I have TWO functions.

我有两个功能。

One hides a modal when the 'close' button is clicked The other function swipes right and changes the modal to another state/view.

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

I am having an issue with the modal NOT 'hiding' when the user swipes to the new state. I can see that it works but the modal remains on display and I was thinking that the best way to approach this is by chaining my two functions? Is this the correct terminology?

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

The TWO functions..

两个功能..

HIDES the modal

隐藏模态

$scope.closetripInfo = function() {
    $scope.modal.hide();
  };

SWIPES right to reveal new .state

SWIPES有权透露新的.state

$scope.onSwipeRight = function() {
  $state.go('app.current-trip');
}

This worked! BUT.. What I was thinking of doing is plugging in my exciting closetripInfo function into the onSwipeRight Like so:

这有效!但是......我想要做的是将令人兴奋的closetripInfo函数插入到onSwipeRight中,如下所示:

$scope.onSwipeRight = function() {

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

It works BUT it does not look right to me and I am getting an error.. I haven't been successful on doing it correctly. Perhaps is not that simple. I also think that I might need to be aware of best practices like promises?

它工作但它看起来不对我,我得到一个错误..我没有成功地做到这一点。也许不那么简单。我还认为我可能需要了解承诺等最佳实践?

THE ERROR

错误

TypeError: $state.go(...).closetripInfo is not a function at Scope.$scope.onSwipeRight

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

Any advice or resources would be greatly appreciated.Thank you!

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

1 个解决方案

#1


4  

You can use AngularJS promises to run functions asynchronously. Promises in AngularJS are provided by the built-in $q service.You can read upon it here.

您可以使用AngularJS promises异步运行函数。 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);

Here is a fiddle on using the $q service. Also this is a good blog post where promises are explained as a cartoon.

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

#1


4  

You can use AngularJS promises to run functions asynchronously. Promises in AngularJS are provided by the built-in $q service.You can read upon it here.

您可以使用AngularJS promises异步运行函数。 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);

Here is a fiddle on using the $q service. Also this is a good blog post where promises are explained as a cartoon.

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