使用选定的行角动力阵列填充输入字段

发布于 2025-02-13 14:57:32 字数 1327 浏览 1 评论 0原文

当我在列表中选择值时,为什么我的第二行从第一行中伸出相同的值?

当第二行添加时,一切都可以,但是它会使我避免使用相同的阀门。因为当我尝试输入一些值时,它可以。

html

<form [formGroup]="Form">
  <div formArrayName="ItemRec">
    <table>
      <thead>
      <tr>
        <th>FORM</th>
      </tr>
      </thead>
      <tbody>
      <tr *ngFor="let ItemRec of ItemRec.controls; let i = index;"
          [formGroupName]="i">
        <td (click)="removeRow(i)" style="width:100px" align="center">
          <i class="fa fa-trash fa-2x"></i></td>
        <td>
          <input style="width:200px" class="form-control"
                 formControlName="dr" ngModel={{transactionCode}}>
        </td>
      </tr>
      </tbody>
    </table>
</form>

.ts

this.Form = this.fb.group({
    Id:['0'],
    ItemRec: this.fb.array([this.CreateItemRec()])
  })
}  


CreateItemRec(): FormGroup {
  return this.fb.group({
    id:[0],
    dr: new FormControl('',Validators.required),
  })
}

get ItemRec(): FormArray {
  return <FormArray>this.Form.get('ItemRec') as 
  FormArray
}


addRow(): void {
  this.ItemRec?.push(this.CreateItemRec());

Why my second row gaves me same values from my first row when i selected value in my list?

all okay when the second row added it gaves me some same val its weird though. cause when i tried to input some value its okay.

HTML

<form [formGroup]="Form">
  <div formArrayName="ItemRec">
    <table>
      <thead>
      <tr>
        <th>FORM</th>
      </tr>
      </thead>
      <tbody>
      <tr *ngFor="let ItemRec of ItemRec.controls; let i = index;"
          [formGroupName]="i">
        <td (click)="removeRow(i)" style="width:100px" align="center">
          <i class="fa fa-trash fa-2x"></i></td>
        <td>
          <input style="width:200px" class="form-control"
                 formControlName="dr" ngModel={{transactionCode}}>
        </td>
      </tr>
      </tbody>
    </table>
</form>

.TS

this.Form = this.fb.group({
    Id:['0'],
    ItemRec: this.fb.array([this.CreateItemRec()])
  })
}  


CreateItemRec(): FormGroup {
  return this.fb.group({
    id:[0],
    dr: new FormControl('',Validators.required),
  })
}

get ItemRec(): FormArray {
  return <FormArray>this.Form.get('ItemRec') as 
  FormArray
}


addRow(): void {
  this.ItemRec?.push(this.CreateItemRec());

The list of my value is in the modal inside of that is mat table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2025-02-20 14:57:32

formcontrolname =“ dr” ngmodel = {{transactionCode}}}

您将模板驱动的形式与反应性形式混合。这可能会有意外的行为。请删除NGMODEL。

我想它将其设置为NGFOR创建的每个字段的默认值。如果应该是默认值,请在形式创建中设置它。

CreateItemRec(): FormGroup {
  return this.fb.group({
    id:[0],
    dr: new FormControl(this.transactionCode, Validators.required),
  })
}

formControlName="dr" ngModel={{transactionCode}}

You are mixing template driven forms with reactive forms. That can have unexpected behavior. Kindly remove the ngModel.

I guess it is set as default value for every field created by ngFor. If it is supposed to be the default value, set it at form creation.

CreateItemRec(): FormGroup {
  return this.fb.group({
    id:[0],
    dr: new FormControl(this.transactionCode, Validators.required),
  })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文