Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
Angular 如何向非';是否为正在注入的数据模型所固有?_Angular_Angular2 Forms - Fatal编程技术网

Angular 如何向非';是否为正在注入的数据模型所固有?

Angular 如何向非';是否为正在注入的数据模型所固有?,angular,angular2-forms,Angular,Angular2 Forms,我无意中发现了另一个关于Angular 2的难题。为了开始在我的表单中添加功能,让我可以告诉Angular根据选择的答案做些什么,我需要学习如何生成反应式表单。现在,我已经让它工作了,并继续利用它的反应性的下一部分,我看到我为单选按钮的名称值设置东西的方式不适用于formControlName,因为我没有将相同的名称=“nameOfThing”对于我想用单选按钮显示的每个选项,我只是在父项中调用它,并将名称值直接绑定到它。这是密码,你知道我在说什么 资料 看到每个单选按钮的名称都必须相同,才能正

我无意中发现了另一个关于Angular 2的难题。为了开始在我的表单中添加功能,让我可以告诉Angular根据选择的答案做些什么,我需要学习如何生成反应式表单。现在,我已经让它工作了,并继续利用它的反应性的下一部分,我看到我为单选按钮的
名称
值设置东西的方式不适用于
formControlName
,因为我没有将相同的
名称=“nameOfThing”
对于我想用单选按钮显示的每个选项,我只是在父项中调用它,并将
名称
值直接绑定到它。这是密码,你知道我在说什么

资料

看到每个单选按钮的名称都必须相同,才能正常工作,我觉得随着时间的推移,数据库将变得越来越大,对更多问题有如此多不同的答案。这是一个本地
const
,我是为了跟随教程而定义的,但在其他方面与FireBase数据库中的内容相同

我在
*ngFor
中对它的称呼是这样的

<input type="radio" name="question.name" <!-- remember it's not in "answers" -->
                    [attr.id]   = "answer.id"
                    [value]= "answer.answer"
/>
我唯一可以合理化的是,我可能必须在
setAnswers()
函数的某个地方想出一些东西。也许像

const name = this.quesform.name;

然后以某种方式将
name
注入
.map()
?或者应该用其他方式吗?

也许这就是你想要的

const answersFGs = answers.map(
    answers => this.fbuild.group(
         Object.assign( {name: this.quesform.name }, answers)));

这给了我一个错误,告诉我
name:
不是
FormGroup
的属性,所以我认为这或多或少是试图手动填充
name
字段,假设它已经存在,而不是创建和定义它。我不确定我是否看到了问题所在,应该从我所看到的情况来看,它显示了此实现的工作版本,不包括
FormGroupName
实现-这是您的问题所在吗?是的,它在没有
FormGroupName
的情况下确实可以工作,但这就是问题所在,我需要使用它来实现答案的功能性使用。
export class QuestionComponent implements OnInit {

    question: QuestionModel;
    quesForm: FormGroup;

    constructor(private qservice : QuestionService, private fbuild : FormBuilder) {}



    ngOnInit(){
        this.qservice.getQuestions().subscribe(Question => {
            this.question = Question;
            this.createForm();
        });

        //console.log(this.question);
        console.log(this.quesForm);
    }

    createForm() {
        this.quesForm = this.fbuild.group({
            question: this.question.question,
            id      : this.question.id,
            name    : this.question.name,
            answers : this.fbuild.array([])
        });
        this.setAnswers(this.question.answers);
    }

    get answers(): FormArray {
        return this.quesForm.get('answers') as FormArray;
    };

    setAnswers(answers : Answers[]){
        const answersFGs = answers.map(answers => this.fbuild.group(answers));
        const answersFormArray= this.fbuild.array(answersFGs);
        this.quesForm.setControl('answers', answersFormArray)
    }


}
const name = this.quesform.name;
const answersFGs = answers.map(
    answers => this.fbuild.group(
         Object.assign( {name: this.quesform.name }, answers)));