簡體   English   中英

在 angular 的嵌套 ngfor 循環中使用 ngmodel 並在 ngModel 中獲取動態值

[英]Using ngmodel in nested ngfor loop in angular and getting dynamic values in ngModel

我正在嘗試使用 angular 創建以下屏幕。 但問題是我無法使用 ngModel 綁定值。 在此處輸入圖像描述

以下是我嘗試過的實現。 提前致謝。

這是我用來綁定值的 model 實體 - patientModule.ts

export class PatientTestDetails {
    public testDate?: string;
    public specimenType?: string;
    public testPrinciple?: string;
    public remark?: string;
    public hemoglobin?: any;
    public wbcCount?: any;
    public rbcCount?: any;
    public meanPlateletVolume?: any;
    public plateletsCount?: any;
    public packedCellVolume?: any;
    public meanCorpuscularVolume?: any;
    public meanCorpuscularHb?: any;
    public meanCorpuscularHbConc?: any; 
    public rbcDistributionWidth?: any;
    public createdDate?: any;
    public modifiedDate?: any;
    public neutrophilsCount?: any;
    public absoluteNeutrophilsCount?: any;
    public eosinophilsCount?: any;
    public absoluteEosinophilsCount?: any;
    public absoluteBasophilsCount?: any;
    public lymphocytesCount?: any;
    public absoluteLymphocytesCount?: any;
    public monocytesCount?: any;
    public absoluteMonocytesCount?: any;
    public basophilsCount?: any
    constructor(
      testDate?: string,
      remark?: string,
      hemoglobin?: any,
      wbcCount?: any,
      rbcCount?: any,
      meanPlateletVolume?: any,
      plateletsCount?: any,
      packedCellVolume?: any,
      meanCorpuscularVolume?: any,
      meanCorpuscularHb?: any,
      meanCorpuscularHbConc?: any,
      rbcDistributionWidth?: any,
      neutrophilsCount?: any,
      absoluteNeutrophilsCount?: any,
      eosinophilsCount?: any,
      absoluteEosinophilsCount?: any,
      absoluteBasophilsCount?: any,
      lymphocytesCount?: any,
      absoluteLymphocytesCount?: any,
      monocytesCount?: any,
      absoluteMonocytesCount?: any,
      basophilsCount?: any
    ) {
      this.testDate = testDate;
      this.specimenType = 'swab';
      this.testPrinciple = 'RTPCR';
      this.remark = remark;
      this.hemoglobin = hemoglobin;
      this.wbcCount = wbcCount;
      this.rbcCount = rbcCount;
      this.meanPlateletVolume = meanPlateletVolume;
      this.plateletsCount = plateletsCount;
      this.packedCellVolume = packedCellVolume;
      this.meanCorpuscularVolume = meanCorpuscularVolume;
      this.meanCorpuscularHb = meanCorpuscularHb;
      this.meanCorpuscularHbConc = meanCorpuscularHbConc;
      this.rbcDistributionWidth = rbcDistributionWidth;
      this.neutrophilsCount = neutrophilsCount;
      this.absoluteNeutrophilsCount = absoluteNeutrophilsCount;
      this.eosinophilsCount = eosinophilsCount;
      this.absoluteEosinophilsCount = absoluteEosinophilsCount;
      this.basophilsCount = basophilsCount;
      this.absoluteBasophilsCount = absoluteBasophilsCount;
      this.lymphocytesCount = lymphocytesCount;
      this.absoluteLymphocytesCount = absoluteLymphocytesCount;
      this.monocytesCount = monocytesCount;
      this.absoluteMonocytesCount = absoluteMonocytesCount;
      this.createdDate = new Date().toISOString();
      this.modifiedDate = new Date().toISOString();
    }
  }

下面是我的組件文件 patientComponent.ts 已在 arrays 之后全局聲明

testDetails = new PatientTestDetails(); // PatientTestDetails is the model specified above
test: any = [];

ngOnInit(): void {
for (let index = 0; index < 6 - 1; index++) { //iterated by 6 as I need 6 columns
   this.test.push(this.testDetails);
 }
}

下面是我的模板文件。 如圖所示,用於生成上述輸入的表格。

<table>
<tr>
   <td *ngFor="let detail of test; let i = index">
     <tr *ngFor="let item of detail | keyvalue">
         <span *ngIf="i === 0;">{{item.key}}</span> 
            <td>
                <input type="text" class="form-control" id="dayWiseData" 
                name="day1" [(ngModel)]="item.value" />
              </td>
          </tr>
          </td>
        </tr>
</table>

寫這個 ngModel 我無法綁定用戶輸入值。 請建議我綁定值的正確方法。 謝謝你。

使用您的代碼,我能夠通過:

<td *ngFor="let detail of test; let i = index">
  <tr *ngFor="let item of detail | keyvalue; let u = index">
    <span *ngIf="i === 0;">{{item.key}}</span>
    <td>
      <input type="text" class="form-control" id="dayWiseData"
                name="day1" [(ngModel)]="test[i][u]" />
    </td>

具有 2 路綁定的 ngModel 必須能夠引用 scope 中的實際值。 一旦嵌套在 arrays 中,就不能將其綁定到內部嵌套引用。 而是(在這種情況下)引用主數據源和該值的路徑。 我在這里使用數據數組的數字索引,但它很容易成為 object 參考[(ngModel)]="test[patient.id][medtest.id]"

如何在 angular 2 中的 ngmodel 上使用二維數組?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM