In Angular, how can I determine if ngModel or form fields are dirty or have input without using formgroup?
I need to check if at least 5 of the input fields have a value or are considered dirty in order to display a message. How can I achieve this for 5 fields from DealFields or ngModel dealDispositionFormFields? Thank you.
#tscode
class DealDispositionFormFields {
dealName:string;
summary:string;
dealDispositionType: string;
terminationPayment:number;
effectiveDate: string;
totalBrokerCommission: number;
effectiveDateString: string;
dealId: number;
}
@Component({
selector: 'app-deal-idle-disposition',
templateUrl: './deal-idle-disposition.component.html',
styleUrls: ['./deal-idle-disposition.component.css']
})
export class DealIdleDispositionComponent implements OnInit {
ngOnInit(): void {
this.dealDispositionFormFields = new DealDispositionFormFields();
}
checkIf5FieldsAreDirty() {
...
}
#html code
<div id="deal-form-container" *ngIf="isEditing">
<div id="deal-form-wizard-top" *ngIf="dealDispositionFormFields.dealDispositionType === 'Buyout'">
<mat-form-field appearance="fill">
<mat-label>Deal Name</mat-label>
<input
matInput
[(ngModel)]="dealDispositionFormFields.dealName"
[required]="isExistingDeal">
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Summary of Deal</mat-label>
<textarea
matInput
class="resize-none"
[(ngModel)]="dealDispositionFormFields.summary"
[rows]="5"
[required]="isExistingDeal">
</textarea>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Termination Payment</mat-label>
<input
matInput
(keyup) = "computeBuyout()"
mask="separator.0"
thousandSeparator=","
[allowNegativeNumbers]="false"
[(ngModel)]="dealDispositionFormFields.terminationPayment"
[required]="isExistingDeal">
<span matPrefix *ngIf="dealDispositionFormFields.terminationPayment">$</span>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Effective Date</mat-label>
<input matInput
(dateChange)="computeBuyout()"
[matDatepicker]="effectiveDate"
[(ngModel)]="dealDispositionFormFields.effectiveDate"
[required]="isExistingDeal">
<mat-datepicker-toggle matSuffix [for]="effectiveDate"></mat-datepicker-toggle>
<mat-datepicker #effectiveDate></mat-datepicker>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Total Broker Commission</mat-label>
<input
matInput
(keyup) = "computeBuyout()"
mask="separator.0"
thousandSeparator=","
[allowNegativeNumbers]="false"
[(ngModel)]="dealDispositionFormFields.totalBrokerCommission"
[required]="isExistingDeal">
<span matPrefix *ngIf="dealDispositionFormFields.totalBrokerCommission">$</span>
</mat-form-field>
</div>
</div>