3

I need a way to set a timeout before the ng-click is triggered, something like:

window.setTimeout("ng-click='myCtrl.myFunction()', 1000")

I don't think it is possibile but I really need to have this kind of effect. I have a very particular animated button, when it is clicked it has to call a function in my controller, but that function has to be invoked only when the buttons animation is finished. (I can't use setTimeout() or something like that inside the function because it keeps the button pressed, and my function has to be executed after the button "get up").

Jongware
  • 22,200
  • 8
  • 54
  • 100
Giulio
  • 67
  • 11
  • `window.setTimeout("ng-click=myCtrl.myFunction()", 1000)` – Rajaprabhu Aravindasamy Mar 23 '16 at 18:33
  • 1
    [$timeout](https://docs.angularjs.org/api/ng/service/$timeout)? – Alon Eitan Mar 23 '16 at 18:35
  • @RajaprabhuAravindasamy nope, already tried, no error but not working. – Giulio Mar 23 '16 at 18:38
  • @AlonEitan I don't think this is possible, my view don't know what it is $timeout, right? – Giulio Mar 23 '16 at 18:40
  • @Giulio No it doesn't - You need to put the code inside the `$timeout` callback function in your controller – Alon Eitan Mar 23 '16 at 18:41
  • Well, you could add the timeout for your function inside the click handler. e.g. function myFunctionHandler() { setTimeout(myFunction, 1000); } But it would be better to do this in an event based way rather than timeouts. That is, have the click handler register an event handler then when the animation is finished an event will be dispatched to your listener. – illvm Mar 23 '16 at 18:41
  • @illvm This is an angularJS question, so recommending to use `setTimeout` is wrong. Angular has its own implementation for this function – Alon Eitan Mar 23 '16 at 18:42
  • 1. Of course you can use $timeout inside control function. Actually you should. 2. There is no 'pressed' state in html, after user clicks on button it takes focus, thats it. Mouse up and down events also triggers. Actually you more likely has problem with your animation. – Petr Averyanov Mar 23 '16 at 18:46
  • Solved. Thanks to all!! – Giulio Mar 23 '16 at 18:50
  • Great! You (or the user who solved it) should post the solution as an answer so the question can be marked as answered. – kolli Mar 23 '16 at 19:11

1 Answers1

0

SOLVED

A user solved the problem but he have deleted his answer, anyway, here is the solution:

$scope.clickOnUpload = function () {
  $timeout(function() {
    angular.element('#myselector').triggerHandler('click');
  }, 100);
};
Giulio
  • 67
  • 11