改变输入值角度 5

5 onchange typescript angular angular5

拜托,你能建议我在更改时如何提交我的值吗?所以,我想amount_paid在更改此值时提交。为了解决这个问题,我创建了一个返回值的函数更改。我需要提交这个值。我试过这个代码:

模板代码:

 <div class="input-field col s2" style="float: right;">
      <label for="amount_paid">Amount Paid:</label>
      <input id="amount_paid" [value]="amountpaid" type="number" class="validate" (change)="updateForm($event)">
 </div>
Run Code Online (Sandbox Code Playgroud)

Ts代码:

    this.addsale = this.fb.group({
      'invoice_number': new FormControl('', [Validators.required, Validators.nullValidator]),
      'client_id': new FormControl('', Validators.required),
      'amount_paid': new FormControl('', Validators.required),
      'products': this.fb.array([]),
    });

  updateForm(ev: any) {
    this.addsale['controls']['amount_paid'].setValue(ev.target.value);
    console.log(ev.target.value)
  }

  onaddsale() {
    let sale = this.addsale.value;
    sale.amount_paid = this.amountpaid; //I used this, because I want to submit this value If I don't want to change. 
    let newSale = new Sale(sale);
    console.log(newSale)
    console.log(this.addsale.controls.amount_paid.value) // Value input when I chane
   }

  get amountpaid() {
    return this.products
      .map(p => p.p_Unit_price * p.p_Quantity)
      .reduce((a, b) => a + b, 0);
  }
Run Code Online (Sandbox Code Playgroud)

请我有 2 种情况,1,我提交值输入,例如 100。这工作正常。和 2. 我在更改此值时提交输入值,因此,从 100 我想更改 90,而这个 90 我想提交。我的函数也提交第一个值sale.amount_paid = this.amountpaid;

更新: 我的问题的演示示例

问题是,amount_paid 字段要修改,修改后的值要保留。如何修改我的值?我想要字段二绑定,如果我不能改变,提交 get amountpaid() {}否则提交我的值输入。

图 1 我保存表单而不更改值。 在此处输入图片说明 图 2 我使用更改后的值保存表单,但该值没有更改。在控制台中显示。

在此处输入图片说明

Lak*_*ker 3

两件事情:

  1. amount_paid 模板应如下所示:

<input formControlName="amount_paid" id="amount_paid" type="number" class="validate">

  1. 在 .ts 中:

    this.addsale.controls['amount_paid'].valueChanges.subscribe(value => { amount_paid is in value and it's called every time the value changes. });

或对于整个表格:

this.addsale.valueChanges.subscribe(value => {
    // whole form object is in value and it's called every time any form field changes.
});
Run Code Online (Sandbox Code Playgroud)

根据评论和您的示例进行编辑:这是工作解决方案。至少我希望这次我能正确理解你的意思。