繁体   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