【发布时间】:2018-10-15 14:39:24
【问题描述】:
我有一个组件:,我需要更新一个表单。在初始化时,我正在调度操作以加载详细信息,并订阅了成功操作,我将在其中创建 formControls。我正在从商店获取数据,但我仍然总是收到此错误,所有形式:
无法读取未定义的属性“名称”
这是出现上述错误 时表单的外观。 这就是表单的外观,。
我试了好几个小时,但不知道我错了。
component.ts
formGroup: FormGroup;
ngOnInit() {
this.store.dispatch(new fromCourse.LoadCardDetails());
this.actionsSubject$
.pipe(filter(action => action.type === CourseActionTypes.LOAD_CARD_DETAILS_SUCCESS))
.subscribe((data: {type, payload: ICardContent } ) => {
this.formGroup = new FormGroup({
name: new FormControl(`${data.payload.name}`, []),
content_type: new FormControl(`${data.payload.content_type}`),
actual_content: new FormControl(`${data.payload.actual_content}`),
content_url: new FormControl(`${data.payload.content_url}`),
additional_content: new FormControl(`${data.payload.additional_content}`),
media_type: new FormControl(`${data.payload.media_type}`),
media_source: new FormControl(`${data.payload.media_source}`),
language_id: new FormControl(`${data.payload.language_id}`),
thumbnail: new FormControl(`${data.payload.thumbnail}`),
});
});
}
get content_type () { return this.formGroup.get('content_type'); }
get name () { return this.formGroup.get('name'); }
get actual_content () { return this.formGroup.get('actual_content'); }
get content_url () { return this.formGroup.get('content_url'); }
get additional_content () { return this.formGroup.get('additional_content'); }
get media_type () { return this.formGroup.get('media_type'); }
get media_source () { return this.formGroup.get('media_source'); }
get language_id () { return this.formGroup.get('language_id'); }
get thumbnail () { return this.formGroup.get('thumbnail'); }
component.html
<form novalidate class="mainForm" [style.fontSize.px]="15" [formGroup]="formGroup">
<div>
<h3 class="sHeading"> Form Type </h3>
<mat-form-field appearance="fill" class="formField">
<mat-select placeholder="Select Type" formControlName="content_type">
<mat-option
#formType
class="option"
*ngFor="let type of types"
[value]="type"
(click)="updateFormType(formType.value)">
{{type}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<div>
<h3 class="sHeading"> Name </h3>
<mat-form-field appearance="outline" class="formField">
<input matInput
placeholder="Name should have 3 characters min."
formControlName="name">
</mat-form-field>
</div>
<div>
<h3 class="sHeading"> Content </h3>
<mat-form-field appearance="outline" class="formField">
<textarea
matInput
placeholder="Describe the content"
rows=6
formControlName="actual_content"></textarea>
</mat-form-field>
</div>
<div class="button-container">
<button mat-raised-button type="submit" class="submitBtn" (click)="onSubmit(formGroup.value)">Update Form</button>
</div>
</form>
提前致谢。
【问题讨论】:
-
您的订阅者函数中显示的
console.log(data)是什么? -
在控制台中,我正在从商店获取正确的数据。 screenshot。但是它在模板中没有正确显示,并且还显示错误(上面已经共享屏幕截图)。