【问题标题】:formControlName input value undefinedformControlName 输入值未定义
【发布时间】:2018-03-13 00:02:54
【问题描述】:

我的 formControlName 有问题,我想提交这个表单,但是一个输入是未定义的,就像在照片中一样。

在控制台中查看我的提交:

我使用了这个代码:

<form [formGroup]="addProductForm" (ngSubmit)="onAddProduct()" class="col s12" materialize style="text-align:center">
    <div class="input-field col s12">
      <input formControlName="Quantity" id="Quantity " type="number" class="validate" [(ngModel)]="Quantity">
             </div>
    <div class="input-field col s12">
      <input formControlName="Unit_price" id="Unit_price" type="number" class="validate" [(ngModel)]="Unit_price">
           </div>
    <div class="input-field col s12">
     <div class="c1" style="text-align:left;">
        Subtotal:
      <input formControlName="Subtotal" id="Subtotal" type="number" class="validate" [(ngModel)]="Subtotal" [value]="Quantity*Unit_price">
    </div>
  </div>

  <br>
  <div id="add_contrat_button_container" class="row">
    <button id="add_contrat_button" type="submit" class="button button1">
      Submit
    </button>
  </div>
</form>

请你看看,问我这段代码有什么问题,在Subtotal设置自动这个值[value]="Quantity*Unit_price"但不要提交。谢谢

【问题讨论】:

  • 您使用formControlNamengModel。删除一个,然后重试。在这两种情况下,您都不需要使用[value]
  • 加 1 表示 trichetriche 的评论。顺便说一句,[value] 仅支持字符串,[ngValue] 支持任何类型
  • @tricheriche 我使用togather 因为,这个值我可以改变,小计也会改变。另外,我尝试并没有任何改变
  • 如果你想进行小计,那么你应该监听(input)事件,不要同时使用两个表单模块。

标签: html angular forms typescript


【解决方案1】:

首先,您需要删除您的 ngModel,因为您已经使用了 formControlName。您可以在此链接angular2 and formControlName value in template 上查看。它可能对您有帮助。而且我不明白你为什么需要这样做。我认为您应该在提交表单后设置 Subtotal 的值是一个更好的解决方案。并将小计的输入更改为普通文本字段以显示它。

【讨论】:

  • 感谢您的回答,如果我没有使用 [(ngModel)] 我不会在 [value]="Quantity*Unit_price" 中显示 Unit_price 和 Quantity
  • 你能写一些代码吗,如何解决这个问题?
猜你喜欢
  • 2017-08-02
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 2013-09-26
  • 1970-01-01
  • 2018-02-18
  • 2018-04-12
相关资源
最近更新 更多