Angular 4:“AbstractControl”类型上不存在属性“push”和“controls”

前端之家收集整理的这篇文章主要介绍了Angular 4:“AbstractControl”类型上不存在属性“push”和“controls”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从这个链接 http://plnkr.co/edit/yV94ZjypwBgHAlb0RLK2?p=preview实现了代码,但得到了推送和控制错误.

这是我做了什么,不知道它有什么问题.

import { Component } from '@angular/core';
import { ViewController,Platform } from 'ionic-angular';
import { FormBuilder,FormGroup,Validators,FormControl,FormArray } from '@angular/forms';

@Component({
  selector: 'filter-vendor',templateUrl: 'filter-vendor.html'
})

export class FilterVendorPage {




  questions = [{id:1,text:'Question 1',answers:[{id:1},{id:2}]},{id:2,text:'Question 2',answers:[{id:11},{id:22}]}]
  surveyForm:FormGroup;



  constructor(
    private viewCtrl: ViewController,private formBuilder:FormBuilder
     ){


      this.surveyForm=this.formBuilder.group({
        question:formBuilder.array([])
      })

      for(var i=0;i<this.questions.length;i++){
          let question=formBuilder.group({
            question_id:[this.questions[i].id,Validators.required],answer_id:formBuilder.array([])
          });
          this.surveyForm.controls['questions'].push(question);
      }
}


   onChange(id,isChecked,index) {

    const answers = <FormArray>this.surveyForm.controls.questions.controls[index].controls.answer_ids

    if(isChecked) {
          answers.push(new FormControl(id))
        } else {
          let idx = answers.controls.findIndex(x => x.value == id)
          answers.removeAt(idx)
    }
  }

}

请帮我解决这个问题.非常感谢

打字稿在类型检查时抱怨.您需要将控件转换为FormArray.所以改变

1)

this.surveyForm.controls['questions'].push(question);

(<FormArray>this.surveyForm.controls['questions']).push(question);

要么

(this.surveyForm.controls['questions'] as FormArray).push(question);

要么

(this.surveyForm.get('questions') as FormArray).push(question);

2)

const answers = <FormArray>this.surveyForm.controls.questions.controls[index].controls.answer_ids

const answers = this.surveyForm.get(['questions',index,'answer_ids']) as FormArray;

要么

const answers = this.surveyForm.get(`questions.${index}.answer_ids`) as FormArray;

Forked Plunker

猜你在找的Angularjs相关文章