错误错误:找不到名称为'[object Object]

时间:2019-02-06 18:47:42

标签: javascript angular angular-reactive-forms

我正在使用Reactive Forms,并且试图将表单传递给子组件,但是遇到了上面的错误。最初,在表单的顶层,我使用FormArray来保存表单,在尝试将其传递给子组件之前,它运行良好。感谢this的发布,我现在知道表单的顶层应该是FormGroup,而FormArray应该是FormGroup的子级。

所以现在我将FormArray嵌套在FormGroup中,并且遇到了上面的错误。我不确定自己在做什么错?在相关代码下面。

//父component.ts

ngOnInit() {
    if (!!this.rows) {

        this.tableForm = new FormArray([]);

        this.rows.forEach((row) => {
            this.rowGroup = new FormGroup({})
            this.columns.forEach(column => {
                this.rowGroup.addControl(column.key, new FormControl(row[column.key]));
            });
            this.tableForm.push(this.rowGroup);
        })

        this.tableGroup = new FormGroup({
            rows: new FormControl(this.tableForm)
        })
    }
}

//父HTML

<section
    *ngIf="!!modal"
    class="modal__mask">
    <section
        class="modal__container"
        #carousel
        [ngStyle]="{'left': start + 'px'}"
        (window:resize)="onResize($event)"
        [formGroup]="tableGroup">

        <div
            *ngFor='let row of selectedRows; let i = index'
            class="modal modal__large"
            [formArrayName]="rows">
            <div
                [formGroupName]="i"
                [ngClass]="{'opacity': modalPage !== i}">
                <div class="modal__header modal__header--large">
                    <h6>Edit Employee Details</h6>
                </div>
                <div class="u-flex u-wrap">
                    <div
                        class="u-flex modal__body"
                        style="width: 50%"
                        *ngFor="let column of columns">
                        <div
                            *ngIf="column.label"
                            class="input__wrapper"
                            [ngClass]="{'input__wrapper--inline': layout === 'horizontal'}">
                            <z-input
                                *ngIf="column.label"
                                class="u-maxX"
                                [group]="tableGroup"
                                [config]="column"> 
                            </z-input>

                            <!-- <div>
                                    <label
                                        class="input__label">
                                        <p class="t-bold t-data">{{column.label}}</p>
                                    </label>
                                    <div class="z-input__default">
                                        <input
                                            class="input u-maxX"
                                         [formControlName]="column.key"
                                            [value]="row[column.key]">
                                    </div>
                                </div> -->

                        </div>
                    </div>
                    <section class="modal__footer u-fillRemaining">
                        <div class="u-flex">
                            <button
                                class="button button--medium"
                                (click)="nextSelectedRow()">
                                <div class="button__content">
                                    <i
                                        class="icon icon--medium"
                                        *ngIf="!!icon">
                                        {{icon}}
                                    </i>
                                    <span>Skip</span>
                                </div>
                            </button>
                        </div>
                        <div class="u-flex">
                            <button
                                class="button button--low"
                                (click)="reset(row, i)">
                                <div class="button__content">
                                    <i
                                        class="icon icon--medium"
                                        *ngIf="!!icon">
                                        {{icon}}
                                    </i>
                                    <span>Reset</span>
                                </div>
                            </button>
                            <button
                                class="button button--low"
                                (click)="saveChanges(row, i)">
                                <div class="button__content">
                                    <i
                                        class="icon icon--medium"
                                        *ngIf="!!icon">
                                        {{icon}}
                                    </i>
                                    <span>Save Changes</span>
                                </div>
                            </button>
                        </div>
                    </section>
                </div>
            </div>
        </div>

//子component.ts

@Input() config;
@Input() group: FormGroup;
@Input() view: string;
@Input() layout: string;

//子HTML

<div
    class="input__wrapper"
    [ngClass]="{'input__wrapper--inline': layout === 'horizontal'}"
    [formGroup]="group"
    [ngSwitch]="config.type">
    <label
        class="input__label"
        *ngIf="!!config.label">
        <p class="t-bold t-data">{{config.label}}</p>
    </label>

    <z-select
        *ngSwitchCase="'select'"
        [config]="config"
        [group]="group"
        [view]="view"
        gridColumn="1 / 5">
    </z-select>

    <div class="z-input__default">
        <input
            *ngSwitchDefault
            class="input u-maxX"
            [formControlName]="config.key"
            [attr.type]="config.type"
            [attr.placeholder]="config.placeholder">
    </div>

0 个答案:

没有答案