0

I have a button on formly with mousedown that has a method called "analyzeClick()"

<button *ngIf="show" (mousedown)="analyzeClick()" id={{htmlID}}>
  {{label}} 
</button> 

now, we also have inputs that have have a (change) event that runs a method called "saveData()"

<input id={{htmlID}} type="text" [formControl]="frmControl" (change)="saveData()"  /> 

However, we found that after you are typing on an input and immediately clicking on the button, (mousedown) runs first before (change) and the problem is that the method on analyzeClick needs data on saveData..

I've tried applying a timeout as specified here

public analyzeClick(): void {
        

        let i = 0;
        (() => {
            if (++i > 1) {
                return;
            }
            setTimeout(function(){
                console.log('Iteration: ' + i);
            }, 15000);

            this.runTheCode();
        })();
}

but still, a button's (mousedown) runs first before an input's (change)

What do I need to do in this scenario to make sure an input's (change) runs first before (mousedown)??

2
  • 1
    wild guess as I'm not sure i totally undestand your problem (the saveData() and analyzeClick() methods are invoked by different HTML elements?): try injecting the angulars changeDetectorRef and call detectChanges() on the saveData() method.
    – scy
    Aug 6, 2021 at 22:10
  • @scy correct they are invoked by different html elements. Ok will try to look for reference for changeDetectorRef and detectChanges(), thansk for the clue! Aug 6, 2021 at 23:33

1 Answer 1

1

There are 4 events to consider:

  • change
  • input
  • mousedown
  • click

input occurs immediately, so that is one way to be absolutely sure that your changes are seen.

mousedown occurs before change - so on your first mouse down, you won't get the entered value

click occurs after change - so that is also safe to use.

See stackblitz for example.

1
  • Much appreciated. We have resolved to use the (click) event instead which runs last after (change) events Aug 10, 2021 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.