我有一个简单的角度模板驱动形式,几乎没有输入,跨度标签和按钮。我需要setValue才能从组件中的对象形成。
我无法将setValue设置为span和button标记,并且出现以下错误。
core.js:1365 ERROR Error: Uncaught (in promise): Error: No value accessor for form control with path: 'userSettings -> id'
Error: No value accessor for form control with path: 'userSettings -> id'
at _throwError (forms.js:1901)
at setUpControl (forms.js:1771)
at resolvedPromise.then (forms.js:3851)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:3988)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138)
at zone.js:870
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3979)
at _throwError (forms.js:1901)
at setUpControl (forms.js:1771)
at resolvedPromise.then (forms.js:3851)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:3988)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138)
at zone.js:870
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3979)
at resolvePromise (zone.js:821)
at zone.js:873
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3979)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:594)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:499)
at invokeTask (zone.js:1536)
at HTMLAnchorElement.globalZoneAwareCallback (zone.js:1562)
我的表单html代码
<form class="form-horizontal" role="form" (ngSubmit)="onSettingUpdateSubmit()" #settingsForm="ngForm" >
<fieldset ngModelGroup="userSettings">
<legend><strong>Account Settings</strong></legend>
<br>
<div class="form-group row">
<label class="col-md-3 col-form-label text-md-right">Id</label>
<div class="col-md-4">
<span class="fw-semi-bold" [ngModel]="id" id="id" name="id">{{ id }}</span>
</div>
</div>
<div class="form-group row">
<label for="firstName" class="col-md-3 col-form-label text-md-right">First Name</label>
<div class="col-md-4">
<input type="text"
id="firstName"
required
[ngModel]="firstName"
name="firstName"
class="form-control"
placeholder="Your first name">
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label text-md-right">Active</label>
<div class="col-md-4">
<button type="button"
id="active"
[value]="active"
[ngModel]="active"
name="active"
[ngClass]="active ? 'btn btn-success' : 'btn btn-danger'"
(click)="onActiveMode()">{{ active ? 'Yes' : 'No'}}</button>
</div>
</div>
</fieldset>
<div class="form-actions">
<div class="btn-block text-center">
<button type="submit"
class="btn btn-primary"
[disabled]=!settingsForm.valid>Update</button>
</div>
</div>
组件中的settingsService将从我的API返回一个对象,如下所示。
{
"id":"12345",
"firstName":"Victor",
"active":true
}
我的settings.compontent.ts
export class SettingsComponent implements OnInit {
ngOnInit() {
this.settingsService.getUserSettings('12345')
.subscribe(
(response: UserSettings) => {
console.log(response);
// this.settingForm.setValue({
// userSettings: response
// });
this.settingForm.form.patchValue({
userSettings: response
});
},
(error) => console.log(error)
);
}
onSettingUpdateSubmit() {
console.log(this.settingForm.value.userSettings);
}
}
当我单击表单中的“更新”按钮时,它将返回所有带有值的完整对象(在我的组件中预先设置用于测试)。
答案 0 :(得分:0)
您似乎正在将某些模板驱动的表单语法与反应式表单语法相混合,因此无法正常工作。
以下是我的模板驱动表单控件之一的示例:
<div class="form-group row">
<label class="col-md-2 col-form-label"
for="firstNameId">First Name</label>
<div class="col-md-8">
<input class="form-control"
id="firstNameId"
type="text"
placeholder="First Name (required)"
required
minlength="3"
[(ngModel)]=customer.firstName
name="firstName"
#firstNameVar="ngModel"
[ngClass]="{'is-invalid': (firstNameVar.touched || firstNameVar.dirty) && !firstNameVar.valid }" />
<span class="invalid-feedback">
<span *ngIf="firstNameVar.errors?.required">
Please enter your first name.
</span>
<span *ngIf="firstNameVar.errors?.minlength">
The first name must be longer than 3 characters.
</span>
</span>
</div>
</div>
请注意,它使用双向绑定:[(ngModel)]=customer.firstName
它绑定到客户对象的firstName属性。在您的情况下,您需要定义一个user
对象。像这样:
export class SettingsComponent implements OnInit {
user: UserSettings;
ngOnInit() {
this.settingsService.getUserSettings('12345')
.subscribe(
(response: UserSettings) => {
console.log(response);
this.user = response;
},
(error) => console.log(error)
);
}
onSettingUpdateSubmit() {
console.log(this.settingForm.value.userSettings);
}
}
还有您的HTML:
[(ngModel)]="user.firstName"
这将自动使用从设置服务中检索的值填充您的表单。
我在这里有一个完整的示例:https://github.com/DeborahK/Angular2-ReactiveForms/tree/master/Demo-Start-Updated