Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bastijns-jeroen/825ed49bd0aad089d911185681858d8c to your computer and use it in GitHub Desktop.

Select an option

Save bastijns-jeroen/825ed49bd0aad089d911185681858d8c to your computer and use it in GitHub Desktop.
@Component({
template: `
<form [formGroup]="form" (ngSubmit)="save()">
<div class="form-group">
<label for="firstname">firstname</label>
<input name="firstname" class="form-control" type="text" formControlName="firstname" />
<div *ngIf="form.get('firstname').touched">
<div *ngIf="form.get('firstname').hasError('required')">This field is required.</div>
</div>
</div>
<div class="form-group">
<label for="lastname">lastname</label>
<input name="lastname" class="form-control" type="text" formControlName="lastname" />
<div *ngIf="form.get('lastname').touched">
<div *ngIf="form.get('lastname').hasError('required')">This field is required.</div>
</div>
</div>
<h4>Delivery address</h4>
<address-form-component (formReady)="addFormControl('deliveryAddress', $event)"></address-form-component>
<h4>Billing address</h4>
<address-form-component (formReady)="addFormControl('billingAddress', $event)"></address-form-component>
<button id="save" type="submit" [disabled]="!form.valid">save</button>
</form>
`
})
export class NewspaperSubscribeComponent implements OnInit {
private form: FormGroup;
constructor(private newspaperService: NewspaperService, private fb: FormBuilder) {}
ngOnInit(): void {
this.form = this.fb.group({
"firstname": new FormControl("", Validators.required),
"lastname": new FormControl("", Validators.required)
});
}
private addFormControl(name: string, formGroup: FormGroup) : void {
this.form.addControl(name, formGroup);
}
public save(): void {
this.newspaperService.createSubscription(this.form.value).subscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment