Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 错误:找不到路径为的控件:_Javascript_Angular_Forms_Typescript_Angular4 Forms - Fatal编程技术网

Javascript 错误:找不到路径为的控件:

Javascript 错误:找不到路径为的控件:,javascript,angular,forms,typescript,angular4-forms,Javascript,Angular,Forms,Typescript,Angular4 Forms,我正在利用Angular 4的FormBuilder和FormGroup类创建表单。表单的内容由GET调用提供,其中的问题和答案通过JSON格式结构化 在使用我的本地主机时,我在网页加载时从web控制台看到以下错误: 有Angular Forms专业知识的人能纠正这个实现并解释我哪里出了问题吗 Web控制台表示HTML中的此节点导致了它: <fieldset [formGroupName]="i"> }从错误中可以看出,您似乎通过HTML中的索引“i”来调用组名,但在初始化中,您

我正在利用Angular 4的FormBuilder和FormGroup类创建表单。表单的内容由GET调用提供,其中的问题和答案通过JSON格式结构化

在使用我的本地主机时,我在网页加载时从web控制台看到以下错误:

有Angular Forms专业知识的人能纠正这个实现并解释我哪里出了问题吗

Web控制台表示HTML中的此节点导致了它:

<fieldset [formGroupName]="i">

}

从错误中可以看出,您似乎通过HTML中的索引“i”来调用组名,但在初始化中,您将其称为“问题”,您在Questions FormArray中只注册了一个组,但迭代了多个组results@yurzui请澄清?我是否需要修改initQuestions()以反映您的建议?Angular不会在FormArray本身中创建项。你应该用手来处理它。现在你有了
Questions:[{QuestionId,AnswerId}]
并在选项卡上循环,这根本不是问题,而是带有问题数组
选项卡的对象数组:[{Questions},{Questions},{Questions}]
Hmmm…手动处理。我不一定需要创建项目,因为问题和答案(例如项目)已经由后端呼叫提供。是否更谨慎的做法是将其附加到一个模型,并在输入上使用一个更改事件,然后选择元素来分配正确的QuestionId和AnswerId?
<form id="form-clinicalscreener" [formGroup]="clinicalForm" (submit)="submitScreenerForm(clinicalForm)" novalidate>
<div formArrayName="Questions">
<div class="section" *ngFor="let tab of clinicalQuestionsResults; let i = index;">
    <h2 class="form-title">{{tab.DisplayText}}</h2>
    <div>
        <div class="form-group" *ngFor="let question of tab.Questions">
            <fieldset [formGroupName]="i">
                <label>{{question.DisplayText}}</label>
                <input type="hidden" formControlName="QuestionId" value="{{question.Id}}" data-value="{{question.Id}}">
                <div [ngSwitch]="question.AnswerType">
                    <div *ngSwitchCase="'Radio'" class="form-group-answers">
                        <div class="radio" *ngFor="let answer of question.Answers">                               
                            <input type="radio" 
                                formControlName="AnswerId" 
                                value="{{answer.Id}}" 
                                data-value="{{answer.Id}}" 
                                class="radio-clinicalscreener">
                            <label class="label-answer">
                                <span class="label-answer-text">{{answer.DisplayText}}</span>
                            </label>
                        </div>
                    </div>
                    <div *ngSwitchCase="'DropDown'" class="form-group-answers">
                        <select formControlName="AnswerId" class="form-control">
                            <option value="{{answer.Id}}" data-value="{{answer.Id}}" *ngFor="let answer of question.Answers">{{answer.DisplayText}}</option>
                        </select>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
</div>
</div>
<div class="text-center">
    <button class="btn btn-screener" type="submit" [disabled]="!clinicalForm.valid">Submit</button>
</div> 
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
selector: '',
templateUrl: '',
styleUrls: ['']
})

export class FormComponent implements OnInit {
// Our model driven form.
clinicalForm: FormGroup;

// Screener Data from API call to be used by view in for loop.
clinicalQuestionsResults: any;

constructor(private formBuilder: FormBuilder) {}

ngOnInit() {
    // Let's get Clinical Screener metadata.
    this.loadClinicalScreener();

    // Initialize the form here.
    this.clinicalForm = this.formBuilder.group({
        Questions: this.formBuilder.array([
            this.initQuestions()
        ])
    });
}

private initQuestions() {
    // initialize our Questions
    return this.formBuilder.group({
        QuestionId: [''],
        AnswerId: ['']
    });
}

private loadClinicalScreener() {
    // An AJAX call from a forkJoin() to return the JSON payload, then I assign it to a local object, screenerResponse. 
    let screenerResponse: any = data[0];
    this.clinicalQuestionsResults = screenerResponse.Result.Tabs;
    console.log('Here is the metadata for Clinical Screener --->', this.clinicalQuestionsResults);
}