使用formGroup和formArray用于UI的问题

时间:2019-03-18 10:08:16

标签: angular angular5 formarray formgroups

我无法使用下面共享的formArray和formGroup UI。

UI

代码

基本的component.html文件

<section class="sampleForm">
<div class="panel">
    <p class="sample-setup-breadcrumbs">
        <a routerLink="/communicationSuite/samples" class="muted-text"> Q & A Listing </a>
        <i class="fa fa-chevron-righta smaller-text muted-text"></i>
        Setup
    </p>

    <div *ngIf="errorOnPage" class="alert alert-danger fade in">
        <a href="#" class="close" data-dismiss="alert" (click)="errorOnPage=false">&times;</a>
        <strong>Error!</strong> {{pageError}}
    </div>
    <form *ngIf="sampleFormGroup"
          [formGroup]="sampleFormGroup"
          (click)="dirtyFormCheck(sampleFormGroup.dirty)"
          (keyup)="dirtyFormCheck(sampleFormGroup.dirty)"
          class="form-horizontal">
        <div class="panel-heading">
            <h4>Name</h4>
        </div>
        <section class="panel-body show-overflow">
            <div class="row col-sm-7">
                <div class="flex-display flex-space-between">
                    <h5 class="muted-text control-header-text">Q & A Name<span class="required">*</span></h5>
                    <label id="nameCharCounter" class="details-text-counter-label muted-text">
                        {{ (sampleFormGroup.controls.sampleName?.value?.length || 0) }} / {{ this.nameMaxLength }}
                    </label>
                </div>
                <input class="form-control text-form-control"
                       formControlName="sampleName" id="sampleName" name="sampleName"
                       value="{{ this.sampleName }}" ng-required="true" ng-trim="false"
                       maxlength="{{ this.nameMaxLength }}" placeholder="Enter Q & A Name" required="required" />

                <div *ngIf="sampleFormGroup && sampleFormGroup.controls.sampleName?.invalid && (sampleFormGroup.controls.sampleName?.dirty || sampleFormGroup.controls.sampleName?.touched)" class="required">sample Name is required.</div>
            </div>
        </section>

        <div class="panel-heading">
            <h4>Question & Answer<span class="required">*</span></h4>
        </div>
        <section class="panel-body show-overflow">
            <div class="row">
                <div class="col-sm-6">
                    <div class="flex-display flex-space-between">
                        <h5 class="muted-text control-header-text">Question Text<span class="required">*</span></h5>
                    </div>
                    <input class="form-control text-form-control"
                           formControlName="questionTitle" id="questionTitle" name="questionTitle"
                           value="{{ this.questionTitle }}" ng-required="true" ng-trim="false" maxlength="{{ this.questionsLength}}}"
                           placeholder="Enter Question Here" required="required" />

                    <div *ngIf="sampleFormGroup && sampleFormGroup.controls.questionTitle?.invalid && (sampleFormGroup.controls.questionTitle?.dirty || sampleFormGroup.controls.questionTitle?.touched)" class="required">sample Question is required.</div>

                    <div class="flex-display flex-space-between">
                        <h5 class="muted-text control-header-text">Answer Type<span class="required">*</span></h5>
                    </div>
                    <!--<select class="form-control" name="questionType" id="questionType" formControlName="questionType">-->
                    <!--<option class="muted-text" ng-selected="this.questionType === 'SINGLE_CHOICE'"> Single Select on Answer Choices </option>-->
                    <!--<option class="muted-text" ng-selected="this.questionType === 'MULTIPLE_CHOICE'"> Multiple Select on Answer Choices</option>-->
                    <!--</select>-->
                    <select class="form-control" name="questionType" id="questionType" formControlName="questionType">
                        <option *ngFor="let questionType of questionTypes" [value]="questionType" class="muted-text">
                            {{ questionType}}
                        </option>
                    </select>

                </div>
                <div class="col-sm-6">
                    <div class="flex-display flex-space-between">
                        <h5 class="muted-text control-header-text">Answer Choices<span class="required">*</span></h5>
                    </div>
                    <input class="form-control text-form-control"
                           formControlName="answerText1" id="answerText1" name="answerText1"
                           value="{{ this.answerText1 }}" ng-required="true" ng-trim="false" maxlength="{{ this.answersLength}}}}"
                           placeholder="Answer Choice 1" required="required" />

                    <div *ngIf="sampleFormGroup && sampleFormGroup.controls.answerText1?.invalid && (sampleFormGroup.controls.answerText1?.dirty || sampleFormGroup.controls.answerText1?.touched)" class="required">At least two answer are required.</div>

                    <input class="form-control text-form-control"
                           formControlName="answerText2" id="answerText2" name="answerText2"
                           value="{{ this.answerText2 }}" ng-required="true" ng-trim="false" maxlength="{{ this.answersLength}}}}"
                           placeholder="Answer Choice 2" required="required" />

                    <div *ngIf="sampleFormGroup && sampleFormGroup.controls.answerText2?.invalid && (sampleFormGroup.controls.answerText2?.dirty || sampleFormGroup.controls.answerText2?.touched)" class="required">At least two answer are required.</div>

                    <input class="form-control text-form-control"
                           formControlName="answerText3" id="answerText3" name="answerText3"
                           value="{{ this.answerText3 }}" ng-required="true" ng-trim="false" maxlength="{{ this.answersLength}}}}"
                           placeholder="Answer Choice 3" />


                    <input class="form-control text-form-control"
                           formControlName="answerText4" id="answerText4" name="answerText4"
                           value="{{ this.answerText4 }}" ng-required="true" ng-trim="false" maxlength="{{ this.answersLength}}}}"
                           placeholder="Answer Choice 4" />


                    <input class="form-control text-form-control"
                           formControlName="answerText5" id="answerText5" name="answerText5"
                           value="{{ this.answerText5 }}" ng-required="true" ng-trim="false" maxlength="{{ this.answersLength}}}}"
                           placeholder="Answer Choice 5" />

                </div>
                <div class="col-sm-7">
                    <div class="flex-display flex-space-between">
                        <h5 class="muted-text control-header-text">Confirmation Message<span class="required">*</span></h5>
                    </div>
                    <input class="form-control text-form-control"
                           formControlName="confirmationMessage" id="confirmationMessage" name="confirmationMessage"
                           value="{{ this.confirmationMessage }}" ng-required="true" ng-trim="false"
                           maxlength="{{ this.confirmationMessageLength }}" placeholder="Type your confirmation message here" required="required" />

                    <div *ngIf="sampleFormGroup && sampleFormGroup.controls.confirmationMessage?.invalid && (sampleFormGroup.controls.confirmationMessage?.dirty || sampleFormGroup.controls.confirmationMessage?.touched)" class="required">Confirmation Message is required.</div>
                </div>
            </div>

        </section>


        <div>All fields required.</div>
        <div class="form-actions btn-container-botton extra-padding">
            <a [routerLink]="['']" class="btn page-secondary-action-btn customer-instance-button">CANCEL</a>
            <button [disabled]="!(sampleFormGroup.valid)" class="btn page-primary-action-btn" type="button" (click)="savesample()">Submit</button>
        </div>
    </form>

</div>

我在使用上述UI的formArray,formGroup初始化表单时遇到麻烦。您能否请人帮忙正确初始化此表单?

示例帖子正文如下所示。如果注意到的话,questions []是模型中的数组,而不是对象,尽管它已在UI中使用过。

{
"samples": [
    {
        "id": 95,
        "name": "Test Self Attestation",
        "type": "SelfAttestation",
        "employerKey": "castlight",
        "externalKey": "test_self_attestation_20190314065907650_9127",
        "questions": [
            {
                "id": 2404,
                "title": "How would you rate your health?",
                "answers": [
                    {
                        "displayOrder": 0,
                        "text": "Excellent",
                        "externalKey": "excellent_20190314065907792_9961",
                        "selected": false
                    },
                    {
                        "displayOrder": 1,
                        "text": "Good",
                        "externalKey": "good_20190314065907792_6599",
                        "selected": false
                    },
                    {
                        "displayOrder": 2,
                        "text": "OK",
                        "externalKey": "ok_20190314065907792_6746",
                        "selected": false
                    },
                    {
                        "displayOrder": 3,
                        "text": "Bad",
                        "externalKey": "bad_20190314065907792_9591",
                        "selected": false
                    },
                    {
                        "displayOrder": 4,
                        "text": "Terrible",
                        "externalKey": "terrible_20190314065907792_1418",
                        "selected": false
                    }
                ],
                "topicName": "Other",
                "topicId": 0,
                "externalKey": "how_would_you_rate_your_health_20190314065907650_7168",
                "isTemplateQuestion": false,
                "question_type": "SINGLE_CHOICE"
            }
        ],
        "confirmationMessage": "Self Attestation completed successfully"
      }
],
"pagination": {
    "page": 0,
    "per_page": 1000,
    "total_pages": 1,
    "total_count": 8
}

}

0 个答案:

没有答案