我正在为移动应用程序构建一个表单。
我使用ion-radio创建了两组单选框。一组用于选择鱼的性别,另一组用于选择成功捕获后鱼的命运。但我无法让这些单选框与formBuilder一起使用。
我收到两条错误消息,分别引用CatchPage.html文件中的第113行和第114行。这两个错误消息是相同的,除了行号不同,因此我只在此处放置一个。
这些错误之后,我得到了什么。
我对我的“catchFate”单选按钮组也收到了类似的错误消息。
来自catches.html的HTML片段。整个.html页面可以在这里查看https://pastebin.com/vKW2vp7F。
整个表单(包括所有字段和单选按钮选择器)都在一个 formGroup 中。
Ionic / Angular 版本
我不明白为什么会出现这些错误。我已经在每个选择中定义了value属性,并且每个选择都具有相同的formControlName。每个选择都在同一个radio-group中。
我的登录页面使用了formBuilder,它运行良好,但是单选框输入让我头疼。
我决定使用formBuilder,因为将来可能需要使用某种验证方式。我只想让功能正常工作并使用addToDb方法将数据发送到firebase,然后再深入进行验证。
我该怎么办?
我使用ion-radio创建了两组单选框。一组用于选择鱼的性别,另一组用于选择成功捕获后鱼的命运。但我无法让这些单选框与formBuilder一起使用。
我收到两条错误消息,分别引用CatchPage.html文件中的第113行和第114行。这两个错误消息是相同的,除了行号不同,因此我只在此处放置一个。
CatchesPage.html:113 ERROR Error: No value accessor for form control with name: 'catchSex'
at _throwError (forms.es5.js:1830)
at setUpControl (forms.es5.js:1740)
at FormGroupDirective.addControl (forms.es5.js:4711)
at FormControlName._setUpControl (forms.es5.js:5299)
at FormControlName.ngOnChanges (forms.es5.js:5217)
at checkAndUpdateDirectiveInline (core.es5.js:10790)
at checkAndUpdateNodeInline (core.es5.js:12216)
at checkAndUpdateNode (core.es5.js:12155)
at debugCheckAndUpdateNode (core.es5.js:12858)
at debugCheckDirectivesFn (core.es5.js:12799)
这些错误之后,我得到了什么。
ERROR Error: Uncaught (in promise): Error: No value accessor for form control with name: 'catchSex'
Error: No value accessor for form control with name: 'catchSex'
at _throwError (forms.es5.js:1830)
at setUpControl (forms.es5.js:1740)
at FormGroupDirective.addControl (forms.es5.js:4711)
at FormControlName._setUpControl (forms.es5.js:5299)
at FormControlName.ngOnChanges (forms.es5.js:5217)
at checkAndUpdateDirectiveInline (core.es5.js:10790)
at checkAndUpdateNodeInline (core.es5.js:12216)
at checkAndUpdateNode (core.es5.js:12155)
at debugCheckAndUpdateNode (core.es5.js:12858)
at debugCheckDirectivesFn (core.es5.js:12799)
at _throwError (forms.es5.js:1830)
at setUpControl (forms.es5.js:1740)
at FormGroupDirective.addControl (forms.es5.js:4711)
at FormControlName._setUpControl (forms.es5.js:5299)
at FormControlName.ngOnChanges (forms.es5.js:5217)
at checkAndUpdateDirectiveInline (core.es5.js:10790)
at checkAndUpdateNodeInline (core.es5.js:12216)
at checkAndUpdateNode (core.es5.js:12155)
at debugCheckAndUpdateNode (core.es5.js:12858)
at debugCheckDirectivesFn (core.es5.js:12799)
at c (polyfills.js:3)
at Object.reject (polyfills.js:3)
at Tab.NavControllerBase._fireError (nav-controller-base.js:322)
at Tab.NavControllerBase._failed (nav-controller-base.js:310)
at nav-controller-base.js:365
at t.invoke (polyfills.js:3)
at Object.onInvoke (core.es5.js:4128)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
我对我的“catchFate”单选按钮组也收到了类似的错误消息。
来自catches.html的HTML片段。整个.html页面可以在这里查看https://pastebin.com/vKW2vp7F。
<ion-row radio-group [(ngModel)]="catchSexSelection" [ngModelOptions]="{standalone: true}">
<ion-item no-lines>
<h2>Sex</h2>
</ion-item>
<!--FISH SEX-->
<ion-col col-4>
<ion-item no-lines>
<ion-label>
<ion-icon name="male"></ion-icon> Male</ion-label>
<ion-radio value="male" formControlName="catchSex"></ion-radio>
</ion-item>
</ion-col>
<ion-col col-4>
<ion-item no-lines>
<ion-label>
<ion-icon name="female"></ion-icon> Female</ion-label>
<ion-radio value="female" formControlName="catchSex"></ion-radio>
</ion-item>
</ion-col>
<ion-col col-4>
<ion-item no-lines>
<ion-label>Unknown</ion-label>
<ion-radio value="unknown" formControlName="catchSex"></ion-radio>
</ion-item>
</ion-col>
</ion-row>
整个表单(包括所有字段和单选按钮选择器)都在一个 formGroup 中。
<form [formGroup]="addCatchForm" (submit)="addToDb()" novalidate>
...Entire form...
</form>
从catches.ts中提取的相关部分
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AuthProvider } from '../../providers/auth/auth';
import { ProfileProvider } from '../../providers/profile/profile';
import * as moment from 'moment';
@IonicPage(
{ name: 'CatchesPage' }
)
@Component({
selector: 'page-catches',
templateUrl: 'catches.html',
})
export class CatchesPage {
addCatchForm: FormGroup;
userProfile: any;
userName: string;
catchSpecies: string;
catchSexSelection: string;
catchWeight: number = 0;
catchLength: number = 0;
catchGirth: number = 0;
catchFate: string;
lureType: string;
fate: boolean;
currentDate: string = moment().format();
constructor(public navCtrl: NavController, public navParams: NavParams, public authProvider: AuthProvider, public profileProvider: ProfileProvider, private alertCtrl: AlertController, public formBuilder: FormBuilder) {
this.addCatchForm = formBuilder.group({
catchCaughtBy: ['', Validators.compose([Validators.required])],
catchDate: ['', Validators.compose([Validators.required])],
catchLocation: ['', Validators.compose([Validators.required])],
catchLocationDesc: [''],
catchSpecies: [''],
catchSexSelection: [''],
catchWeight: [''],
catchLength: [''],
catchGirth: [''],
lureModel: [''],
lureType: [''],
lureTechnique: [''],
catchFate: ['']
});
}
Ionic / Angular 版本
Ionic Framework: 3.3.0
Ionic App Scripts: 1.3.7
Angular Core: ^4.1.2
Angular Compiler CLI: ^4.1.2
Node: 7.10.0
OS Platform: Windows 10
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
我不明白为什么会出现这些错误。我已经在每个选择中定义了value属性,并且每个选择都具有相同的formControlName。每个选择都在同一个radio-group中。
我的登录页面使用了formBuilder,它运行良好,但是单选框输入让我头疼。
我决定使用formBuilder,因为将来可能需要使用某种验证方式。我只想让功能正常工作并使用addToDb方法将数据发送到firebase,然后再深入进行验证。
我该怎么办?
ngModel
和ngModelOptions
。 - developer033