I am returning a value from a dialog box (MatDialogRef) to an input tag. Even though I am changing the value of the input tag, the change event is not getting triggered.
I have tried using the dispatchEvent. All of the documentation and examples I have found show the creation of the event and then triggering. I do not wish to create the event.
Others show the callback within the code. I cannot call the method within the html view change from the component without getting errors on missing parameters.
I have looked at doing this with vanilla JS and TypeScript.
// dialog box opens up clock-like interface to allow the end user to select a start and stop time
// I am using ViewChild to grab the DOM elements
// ViewChild to access startTime and stopTime DOM elements
@ViewChild( 'startTime' ) elStartTime: ElementRef;
@ViewChild( 'stopTime' ) elStopTime: ElementRef;
// This method runs when the ok button is clicked that closes the dialog
public calcHours (): void {
// Variables to capture timeClock entries from DOM. Total variable to house calculation
let currentStartTime = this.elStartTime.nativeElement.value;
let currentStopTime = this.elStopTime.nativeElement.value;
// Cast the returned time from string to number
currentStartTime = +( currentStartTime.slice( 0, -( currentStartTime.indexOf( ':' ) + 1 ) ) );
currentStopTime = +( currentStopTime.slice( 0, -( currentStopTime.indexOf( ':' ) + 1 ) ) );
// Math to calculate the number of hours based upon the returned string time
const totalHours = currentStopTime - currentStartTime;
if ( currentStartTime !== '' && currentStopTime !== '' ) {
this.dialogRef.close( totalHours );
}
}
// On the main component, I am using ViewChild to grab the input
// ViewChild to access hours worked DOM elements
@ViewChild( 'hoursWorked' ) elHoursWorked: ElementRef;
// When the dialog closes, this method runs and changes the hoursWorked input
dialogRef.afterClosed().subscribe( result => {
// Value returned from dialog box
const dialogHoursCalc = `${ result }`;
// Set value of input field from dialog box
this.renderer.setProperty( this.elHoursWorked.nativeElement, 'value', dialogHoursCalc );
} );
// My html template input tag is as follows...
<input class="form-control" type="number" step="0.01" data-number-to-fixed="2" placeholder="0.00"
(change)="onRecordUpdated(timeCard, timecarddet)" [(ngModel)]="timecarddet.hours" min="0"
max="365" #hoursWorked>
Expectation - The end user opens up the dialog box with the clock-like interface. They select a start and stop time. They click the ok button. The dialog closes and the hoursWorked input field is updated. The updating of the input field triggers the change event and the onRecordUpdated method runs.
Actual - The end user opens up the dialog box with the clock-like interface. They select a start and stop time. They click the ok button. The dialog closes and the hoursWorked input field is updated. The change event is never triggered and the onRecordUpdated method never runs.
How do I get the change event to trigger and have the onRecordUpdated method run?