问题
I'm creating a form to edit clients but I'm getting alot of errors for some reason. The first error is the "Error: formGroup expects a FormGroup instance. Please pass one in.".
Then in addition im gettin "ERROR TypeError: Cannot read property 'get' of undefined" for every input line in my HTML.
I'm using pretty much the same configuration in another component and it works fine, I can't understand why this is throwing me errors.
this.myGroup = new FormGroup({
firstName: new FormControl()
});
<form [formGroup]="modifyClientForm" [ngClass]="{'view-only': inViewMode}" (ngSubmit)="modifyClient()">
<!--BLOCK 1: TAB 1 start-->
<div *ngIf="activeTab==='clientName'" class="white-bg">
<div class="form-row">
<div class="form-group col-12">
<label for="companyName">Ettevõtte nimi<span *ngIf="!inViewMode" class="required">*</span></label>
<input class="form-control" formControlName="companyName" name="companyName" id="companyName">
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-12 col-md-6">
<label for="firmRegNo">Reg number<span *ngIf="!inViewMode" class="required">*</span></label>
<input formControlName="firmRegNo" class="form-control" name="firmRegNo" id="firmRegNo">
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-12">
<label for="address">Aadress</label>
<input class="form-control" id="address" name="address" formControlName="address" >
</div>
</div>
<h4 class="form-title-mt">Kontakt</h4>
<div class="form-row">
<div class="form-group col-sm-12">
<label for="clientName">Kliendi nimi</label>
<input class="form-control" id="clientName" name="clientName" formControlName="clientName">
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-6">
<label for="phoneOne">Telefon 1</label>
<input class="form-control" id="phoneOne" name="phoneOne" formControlName="phoneOne">
</div>
<div class="form-group col-sm-6">
<label for="phoneTwo">Telefon 2</label>
<input class="form-control" id="phoneTwo" name="phoneTwo" formControlName="phoneTwo" >
</div>
</div>
<div class="form-row">
<div class="form-group col-sm-12">
<label for="email">Email</label>
<input class="form-control" id="email" name="email" formControlName="email">
</div>
</div>
<div class="form-row mt-3">
<div class="form-group col-sm-12">
<label for="explanation">Selgitus</label>
<textarea class="form-control" id="explanation" name="explanation" placeholder="Hetkel puudub" formControlName="explanation"></textarea>
</div>
</div>
</div>
<!--BLOCK 1: TAB 1 end-->
<!--BLOCK 1: TAB 2 start-->
<div *ngIf="activeTab==='contract'" class="white-bg">
<app-client-contracts [activeClient]="activeClient"></app-client-contracts>
</div>
<!--BLOCK 1: TAB 2 end-->
<!--BLOCK 1: TAB 3 start-->
<div *ngIf="activeTab==='files'" class="white-bg">
<!-- Todo: kliendi failid -->
</div>
<!--BLOCK 1: TAB 3 end-->
The typescript:
modifyClientForm: FormGroup;
activeClient: Client;
constructor(private clientService: ClientService, private formBuilder: FormBuilder) { }
ngOnInit() {
this.inViewMode = true;
this.clientId = parseInt(window.location.pathname.split('/')[2], 10);
this.clientService.getClientById(this.clientId)
.subscribe(data => this.activeClient = data);
this.modifyClientForm = this.formBuilder.group({
companyName: [this.activeClient.companyName],
firmRegNo: [this.activeClient.firmRegNo],
address: [this.activeClient.address],
clientName: [this.activeClient.clientName],
phoneOne: [this.activeClient.phoneOne],
phoneTwo: [this.activeClient.phoneTwo],
email: [this.activeClient.email],
explanation: [this.activeClient.explanation]
});
}
回答1:
The problem is that your html renders before the formgroup is initialized.
You can solve this problem by placing the form inside a div and use *ngIf like so:
<div *ngIf="modifyClientForm">
<form [formGroup]="modifyClientForm" [ngClass]="{'view-only': inViewMode}"
(ngSubmit)="modifyClient()">
....other code...
</form>
<div>
I hope this helps someone!
来源:https://stackoverflow.com/questions/54571214/error-formgroup-expects-a-formgroup-instance-please-pass-one-in