0

Currently I have a textbox and I am using a controller method inside ng-enter directive to save the data entered in the text box.

<input ng-enter="callMeToSave()"/>

The issue is when I press enter button twice by mistake the controller method mentioned inside ng-enter is being called twice. Is there a way to restrict this from happening? Any help would be much appreciated.

  • Hi. Maybe you can avoid execute JS method twice or more times: https://stackoverflow.com/questions/23119282/prevent-javascript-function-from-running-twice-settimeout – mggSoft Oct 19 '18 at 06:23
  • @mggSoft Thanks for the help. With your and holydragon's suggestions I derived a custom logic to stop the execution twice :) – Sathiya Narayanan Oct 19 '18 at 07:53

1 Answers1

1

You can use $timeout with a flag in ng-enter so that when you press enter the first time, you set the flag (something like enterPressed) to true and use $timeout to set it to false when the interval is passed. When you press enter the next time(s), make it check the flag enterPressed. If true then ignore it because you have just pressed enter; otherwise, this is the first time you press enter in a while, so execute your function here.

holydragon
  • 6,158
  • 6
  • 39
  • 62