在Angular 2和Typescript

时间:2016-08-18 11:58:26

标签: html arrays json angular typescript

在Angular 2中,我想知道是否可以定义一个包含多个其他数组的数组。显示我的意思可能更容易:

我开始使用它:

export class PaymentDetails {
     account: any[];
     bpNumber: number;
}

但这给了我一个问题,当我填充它时,我无法真正访问帐户数组中的数据,因为我想要更多的数组。

所以现在我想像这样定义我的课程:

export class PaymentDetails {
    account: [
        debitAccount: [
            {
                debitAccountId: string;
                debitBankCode: string;
                debitBankName: string;
                debitCountryCode: string;
                debitAccountNumber: string;
                debitAccountHolder: string;
                debitContinous: string;
                debitDueDate: string;
                iban: string;
                bic: string;
            }
        ],
        ccAccount: [
            {
                ccAccountId: string;
                ccCompanyCode: string;
                ccNumber: string;
                ccStart: string;
                ccExpiry: string;
                ccDsbTransactionId: string;
                ccCardholderName: string
            }
        ]
    ];
    bpNumber: number;

}

这一切都可能吗?

该类正在填充此InMemoryDataService

export class InMemoryDataService {
  createDb() {
let paymentDetailsDB = [
      {
        account: [
          {
            debitAccount: [
              {
                debitAccountId: '8736583',
                debitBankCode: '45345',
                debitBankName: 'KSK HGTT',
                debitCountryCode: 'DE',
                debitAccountNumber: '123453463',
                debitAccountHolder: 'A Berg',
                debitContinous: '',
                debitDueDate: '',
                iban: 'DE12344235',
                bic: '324645',
              },
              {
                debitAccountId: '6567456',
                debitBankCode: '55463453',
                debitBankName: 'GRDFE',
                debitCountryCode: 'DE',
                debitAccountNumber: '000123492',
                debitAccountHolder: 'A Berg',
                debitContinous: '',
                debitDueDate: '',
                iban: 'DE43523453',
                bic: '123547665',
              }
            ],

            ccAccount: [
              {
                ccAccountId: '23413',
                ccCompanyCode: '254345',
                ccNumber: '238857827368837',
                ccStart: '2010-10-05',
                ccExpiry: '2018-10-05',
                ccDsbTransactionId: '235231',
                ccCardholderName: 'Anne Berg',
              }
            ],
          }
        ],
        bpNumber: 4711,
      }
    ];
    return {paymentDetailsDB};
  }
}

2 个答案:

答案 0 :(得分:3)

您的定义应如下所示:

匿名类型变体:

export class PaymentDetails {
  accounts:
  {
    debitAccounts:
    {
      debitAccountId: string;
      debitBankCode: string;
      debitBankName: string;
      debitCountryCode: string;
      debitAccountNumber: string;
      debitAccountHolder: string;
      debitContinous: string;
      debitDueDate: string;
      iban: string;
      bic: string;
    }[],

    ccAccounts:
    {
      ccAccountId: string;
      ccCompanyCode: string;
      ccNumber: string;
      ccStart: string;
      ccExpiry: string;
      ccDsbTransactionId: string;
      ccCardholderName: string
    }[],

    bpNumber: number;
  };
}

命名类型(你应该使用它!:])

export class DebitAccount {
  Id: string;
  BankCode: string;
  BankName: string;
  CountryCode: string;
  Number: string;
  Holder: string;
  Continous: string;
  DueDate: string;
  iban: string;
  bic: string;
}

export class CcAccount {
  Id: string;
  CompanyCode: string;
  Number: string;
  Start: string;
  Expiry: string;
  DsbTransactionId: string;
  CardholderName: string
}

export class Account {
  debitAccounts: DebitAccount[];
  ccAccounts: CcAccount[];
  bpNumber: number;
}

export class PaymentDetails {
  account: Account[];
}

答案 1 :(得分:0)

嵌套数组可以定义如下:

  items = [
     {name:'xyz', subMenuItem:[{name:'abc'}, {name:'cde'}], icon:'home'},
     {name:'pqr', subMenuItem:[{name:'abc'}, {name:'cde'}], icon:'home'},
    ];