gpt4 book ai didi

html - 为什么我在 Angular react 形式的 *ngIf 中出现错误

转载 作者:太空狗 更新时间:2023-10-29 18:27:40 26 4
gpt4 key购买 nike

我知道这是一个非常愚蠢的问题,对于以我的 SOF 声望的人来说不应该问。但是我正在拉扯我的头发并且似乎无法理解我做错了什么。我指的是 Stackblitz 上的一个例子创建 react 形式。

在 Stackblitz 中,一切似乎都很好,我的代码也能正常工作。但是在 vs 代码中,我看到错误 Identifier 'nameType' is not defined。 '__type' 不包含这样的 memberAngular
代码:

 <div>
<label>Name Type</label>
<mat-select formControlName="nameType" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.prefix.errors }">
<mat-option *ngFor="let obj of nameTypeSelection" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
</mat-select>
<div *ngIf="submitted && f.nameType.errors" class="invalid-feedback">
<div *ngIf="f.nameType.errors.required">Name Type is required</div>
</div>

  // Form Variable for Ind Cust
indCustregisterForm: FormGroup;
submitted = false;

ngOnInit() {
this.indCustregisterForm = this.formBuilder.group({
prefix: ['', Validators.required],
nameType: ['', Validators.required],
firstName: ['', Validators.required],
middleName: [''],
lastName: ['', Validators.required],
gender: ['', Validators.required],
dateOfBirth: ['', Validators.required],
citizenship: ['', Validators.required]
});
}
// convenience getter for easy access to form fields of Individual Customer Form
get f() {
return this.indCustregisterForm.controls;
}

onSubmit() {
this.submitted = true;
if (this.indCustregisterForm.valid) {
console.log('Success')
}

// stop here if form is invalid
if (this.indCustregisterForm.invalid) {
console.log('Error')
});
return;
}

}
<!-- Individual Customer Form -->
<form [formGroup]="indCustregisterForm" (ngSubmit)="onSubmit()">
<div>
<label>Name Type</label>
<mat-select formControlName="nameType" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.prefix.errors }">
<mat-option *ngFor="let obj of nameTypeSelection" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
</mat-select>
<div *ngIf="submitted && f.nameType.errors" class="invalid-feedback">
<div *ngIf="f.nameType.errors.required">Name Type is required</div>
</div>
</div>

<div>
<label>Prefix</label>
<mat-select formControlName="prefix" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.prefix.errors }">
<mat-option *ngFor="let obj of prefixes" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
</mat-select>
<div *ngIf="submitted && f.prefix.errors" class="invalid-feedback">
<div *ngIf="f.prefix.errors.required">Prefix is required</div>
</div>
</div>

<div>
<label>First Name</label>
<input type="text" formControlName="firstName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.firstName.errors }" />
<div *ngIf="submitted && f.firstName.errors" class="invalid-feedback">
<div *ngIf="f.firstName.errors.required">First Name is required</div>
</div>
</div>

<div>
<label>Middle Name (Optional)</label>
<input type="text" formControlName="middleName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.middleName.errors }" />
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" formControlName="lastName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }" />
<div *ngIf="submitted && f.lastName.errors" class="invalid-feedback">
<div *ngIf="f.lastName.errors.required">Last Name is required</div>
</div>
</div>

<div>
<label>Gender</label>
<mat-select formControlName="gender" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.gender.errors }">
<mat-option *ngFor="let obj of genders" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
</mat-select>
<div *ngIf="submitted && f.gender.errors" class="invalid-feedback">
<div *ngIf="f.gender.errors.required">Gender is required</div>
</div>
</div>
<div>
<input
matInput
[min]="minDate"
[max]="maxDate"
[matDatepicker]="picker"
formControlName="dateOfBirth"
placeholder="Choose Date of Birth"
/>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<div *ngIf="submitted && f.dateOfBirth.errors" class="invalid-feedback">
<div *ngIf="f.dateOfBirth.errors.required">Date of Birth is required</div>
</div>
</div>
<div>
<label>Citizenship</label>

<mat-select formControlName="citizenship" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.prefix.errors }">
<mat-option *ngFor="let obj of citizenships" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
</mat-select>
<div *ngIf="submitted && f.citizenship.errors" class="invalid-feedback">
<div *ngIf="f.citizenship.errors.required">Citizenship is required</div>
</div>
</div>
<div class="form-group">
<button>Save</button>
</div>
</form>

我附上了我的屏幕快照。为什么我在 VS Code 中出错。 enter image description here

最佳答案

而不是写

<div *ngIf = "f.nameType.errors.required"> 

你应该写

<div *ngIf = "indCustregisterForm.controls['nameType']?.errors.required">

关于html - 为什么我在 Angular react 形式的 *ngIf 中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56174366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com