Ionic Framework - forEach on Class of Class instance

时间:2018-01-08 23:01:54

标签: angularjs ionic-framework

尝试遍历下面的forEach,但它无法正常工作并完全跳过它。我没有得到任何控制台错误,并且打印出FormGroup显示没有添加任何控件。

我需要帮助找出如何从下面的字段创建者服务迭代类实例数组。

形状control.service.ts

import { Injectable }   from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

import { FormBase } from './form-base';

@Injectable()
export class FormControlService {
  constructor() { }

  toFormGroup(items: FormBase<any>[] ) {
    let group: any = {};

    console.log("GETS HERE");

    items.forEach(item => {

      console.log("DOESNT GET HERE");

      group[item.key] = item.required ? new FormControl(item.value || '', Validators.required) : new FormControl(item.value || '');

    });

    console.log("GETS HERE");

    return new FormGroup(group);
  }
}

这是我的数据模型:

形状base.ts

export class FormBase<T>{
  value: T;
  key: string;
  label: string;
  required: boolean;
  order: number;
  controlType: string;

  constructor(options: {
      value?: T,
      key?: string,
      label?: string,
      required?: boolean,
      order?: number,
      controlType?: string
    } = {}) {
    this.value = options.value;
    this.key = options.key || '';
    this.label = options.label || '';
    this.required = !!options.required;
    this.order = options.order === undefined ? 1 : options.order;
    this.controlType = options.controlType || '';
  }
}

这是我的字段创建者服务:

field.service.ts

import { Injectable } from '@angular/core';

import { FormBase } from '../app/form-base'; 
import { DropdownField } from '../app/form-dropdown';
import { TextareaField } from '../app/form-textarea';
import { RadioField } from '../app/form-radio';

import { AuthAPI } from './auth-api/auth-api';

@Injectable()
export class FormFieldsService {

  constructor(private auth: AuthAPI) { }

  getFields() {

    let fields: FormBase<any>[] = []; 

    this.auth.getAppConfig().subscribe((data) => {

        let loc_config = [];
        for(let loc in this.auth.activeConfig.locations) {
            loc_config.push({'key':this.auth.activeConfig.locations[loc].id, 'value':this.auth.activeConfig.locations[loc].name});
        }
        let order = 1;
        fields.push(new DropdownField({
          key: 'location',
          label: 'LOCATION',
          options: loc_config,
          value: '',
          order: order
        }));

        let tip_data_config = [];
        for(let h in this.auth.activeConfig.tipTypes) {
          let slug = this.auth.activeConfig.tipTypes[h].title;
          slug = slug.replace(/([\/ |\-\:\\])/g,'_').toLowerCase();
          order = order + 1;
          for(let i in this.auth.activeConfig.tipTypes[h].groups) {
            tip_data_config.push({'key': this.auth.activeConfig.tipTypes[h].groups[i].name, 'value': this.auth.activeConfig.tipTypes[h].groups[i].value });
          }

          fields.push(new DropdownField({
            key: slug,
            label: this.auth.activeConfig.tipTypes[h].title,
            options: tip_data_config,
            value: '',
            order: order
          }));

          order = order + 1;
          fields.push(new RadioField({
            key: slug+'_rating',
            label: '',
            value: '',
            type: 'radio',
            required: true,
            order: order
          }));

          order = order + 1;
          fields.push(new TextareaField({
            key: slug+'_text',
            label: '',
            value: '',
            order: order
          }));
        }

        order = order + 1;
        fields.push(new RadioField({
          key: 'work_activity',
          label: null,
          type: 'radio',
          required: true,
          value: '',
          order: order
        }));

        order = order + 1;
        fields.push(new TextareaField({
          key: 'work_activity_text',
          label: '',
          value: '',
          order: order
        }));

        order = order + 1;
        fields.push(new RadioField({
          key: 'property_type',
          label: null,
          type: 'radio',
          value: '',
          required: true,
          order: order
        }));

        order = order + 1;
        fields.push(new TextareaField({
          key: 'property_class_text',
          label: '',
          value: '',
          order: order
        }));

    });  

    console.log('fields:',fields.sort((a, b) => a.order - b.order));

    return fields.sort((a, b) => a.order - b.order);
  }
}

项目数组: items.forEach

0 个答案:

没有答案