【问题标题】:angular 8 how to pass parent class value to child class角度8如何将父类值传递给子类
【发布时间】:2021-03-14 11:08:41
【问题描述】:

我想为buyer.cnic、buyer.name 和buyer.phone 分配价值数据。 我得到的值是 {sCommercial: true ​​​ 玛拉:“12” ​​​ 总金额:“0121233123” ​​​ }

export class AddDhaFile {
  buyer: Buyer[];
  dealNumber?: string;
  marla: number;
  isCommercial: boolean;
  totalAmount: number;
  payments?: Payment[];
  constructor(value: any) {
    this.marla = value.marla;
    this.isCommercial = value.isCommercial;
    this.totalAmount = value.totalAmount;
  }
}

export class Buyer {
  name: string;
  cnic: string;
  phone: string;
  constructor(value: any) {
    this.name = value.name;

    this.cnic = value.cnic;
    this.phone = value.phone;
  }
}

【问题讨论】:

    标签: angular typescript interface


    【解决方案1】:

    如果您想将数据从父母传递给孩子,您可以通过 3 种方式进行。 https://angular.io/guide/component-interaction 这是有关如何执行此操作的链接。

    【讨论】:

      【解决方案2】:

      我假设值对象看起来像这样。

      value = {
      isCommercial: true,
      ​marla: "12",
      ​totalAmount: "0121233123"​,
      buyer: {name: 'xyx' , cnic: 'some digits', phone:'0900 78601'}
      }
      
      
      export class AddDhaFile {
        buyer: Buyer[];
        dealNumber?: string;
        marla: number;
        isCommercial: boolean;
        totalAmount: number;
        payments?: Payment[];
        constructor(value: any) {
          this.marla = value.marla;
          this.isCommercial = value.isCommercial;
          this.totalAmount = value.totalAmount;
          this.buyer.append(new Buyer(value.buyer))
        }
      }
      

      或者如果值对象看起来像这样。

      value = {
      isCommercial: true,
      ​marla: "12",
      ​totalAmount: "0121233123"​,
      buyers: [{...}, {...}, {...}]
      }
      
      export class AddDhaFile {
        buyer: Buyer[];
        dealNumber?: string;
        marla: number;
        isCommercial: boolean;
        totalAmount: number;
        payments?: Payment[];
        constructor(value: any) {
          this.marla = value.marla;
          this.isCommercial = value.isCommercial;
          this.totalAmount = value.totalAmount;
          this.buyer = value.buyers.map(buyer => new Buyer(buyer)))
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        相关资源
        最近更新 更多