I am using Template Driven Form.
Parent Component HTML
<form #BasicForm="ngForm" (ngSubmit)="onBasicDetailsSubmit()" id="BasicForm">
<app-input-text [(sharedVar)]="dashboardDetails.Text1" [isMandatory]="true" ></app-input-text>
<app-input-text [(sharedVar)]="dashboardDetails.Text2" [isMandatory]="false"></app-input-text>
<input type="submit" value="Save" [disabled]="!BasicForm.valid" class="btn btn-success">
</form>
Child Component
TS
@Input() sharedVar: number;
@Input() isMandatory: boolean;
@Output() sharedVarChange = new EventEmitter();
change(newValue) {
this.sharedVar = newValue;
this.sharedVarChange.emit(newValue);
}
HTML
<input type="text" class="form-control" [(ngModel)]="sharedVar" (ngModelChange)="change($event)" [attr.required]="isMandatory">
The Submit button is not getting disabled. I have tried writing required
in child component as well as parent component selector, but it doesn't work. Please help.
form
between parent and child. stackoverflow.com/questions/51281527/… Only difference is you have template driven forms. That should not be an issue. Once done, you can fetch form controls to validate in either of the components – Amit Chigadani