错误TS2339:类型"DataModel []"上不存在属性"ITEMS" [英] Error TS2339: Property 'ITEMS' does not exist on type 'DataModel[]'

查看:109
本文介绍了错误TS2339:类型"DataModel []"上不存在属性"ITEMS"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误消息: TS2339:类型"DataModel []"上不存在属性"ITEMS"

I get this error message: TS2339: Property 'ITEMS' does not exist on type 'DataModel[]'

我正在使用IONIC/ANGULAR,并使用模型进行反应式表单,并从HTTP获取数据.

I'm working in IONIC/ANGULAR and doing a reactive Form with a model and getting data from HTTP.

存在模型(model.ts):

export class DataModel {
    public ITEMS: any[];

    constructor(public NAME: string, public QUANTITY: Array<string>) {
        this.ITEMS = [{NAME, QUANTITY}];
    }
}

form.page.ts

  setItems() {
    const control = this.itemsForm.controls.items as FormArray;
    this.dbData.ITEMS.forEach(element => {
      control.push(
        this.formBuilder.group({
          name: element.NAME,
          quantity: this.setItem(element)
        })
      );
    });
  }

有完整的代码表单.page.ts

export class Form2Page implements OnInit, OnDestroy {
  obsSubscription: Subscription;
  itemsForm: FormGroup;

  public dbData: DataModel[];


  constructor(
    private formBuilder: FormBuilder,
    private storageService: FormStorageService
  ) {}

  ngOnInit() {
    // Get data from DB
    this.storageService.getDBData();
    this.obsSubscription = this.storageService.dbSubject.subscribe(
      (value: any[]) => {
        this.dbData = value;

    // Creating the FORM
        this.initForm();
        this.setItems();
      },
      error => {
        console.log('erreur');
        console.log(error);
      }
    );
  }

  initForm() {
    this.itemsForm = new FormGroup({
      items: new FormArray([])
    });
  }

  setItems() {
    const control = this.itemsForm.controls.items as FormArray;
    this.dbData.ITEMS.forEach(element => {
      control.push(
        this.formBuilder.group({
          name: element.NAME,
          quantity: this.setItem(element)
        })
      );
    });
  }

  setItem(value) {
    const array = new FormArray([]);
    value.QUANTITY.forEach(element => {
      array.push(this.formBuilder.control(element));
    });
    return array;
  }

  findFieldArray(field: any): FormArray {
    return this.itemsForm.get(field) as FormArray;
  }

  addNewItems() {
    const control = this.itemsForm.controls.items as FormArray;
    control.push(
      this.formBuilder.group({
        name: '',
        quantity: this.formBuilder.array([this.formBuilder.control('')])
      })
    );
  }

  addNewItem(item: FormGroup) {
    (item.get('quantity') as FormArray).push(this.formBuilder.control(''));
  }

  removeItem(item: FormGroup, qtyIndex: number, itemIndex: number) {
    (item.get('quantity') as FormArray).removeAt(qtyIndex);
  }

  removeItems(itemIndex: number) {
    const form = this.itemsForm.get('items') as FormArray;
    form.removeAt(itemIndex);
  }

  checkArray(itemIndex: number) {
        // Get the quantity array lenght to display the remove ITEMS button
        return ((this.itemsForm.get('items') as FormArray)
        .at(itemIndex)
        .get('quantity') as FormArray).length;
  }

  onFormSubmit() {
    console.log('Submit : ', this.itemsForm.value);
  }

  ngOnDestroy() {
    this.obsSubscription.unsubscribe();
  }
}

感谢您的帮助

推荐答案

dbData是DataModel[],您可以以this.dbData.ITEMS的身份访问它(肯定不存在).所以要么必须

dbData is a DataModel[] , you access it as this.dbData.ITEMS which for sure doesn't exists. so either you have to

dbData : DataModel;

this.dbData.ITEMS...

dbData : DataModel[];

this.dbData.forEach(dbItem => {

   dbItem.ITEMS...
})

我也注意到DataModel里面有一个数组,我想你的意思是要有一个DataModel而不是一个数组

also I noticed DataModel has an array inside, I guess you meant to have one DataModel not an array

这篇关于错误TS2339:类型"DataModel []"上不存在属性"ITEMS"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆