[英]change input value angular 5
Please, can you suggest me how to submit my value when I change it? 拜托,我可以建议我在更改时如何提交我的价值? So, I want to submit
amount_paid
when I change this value. 所以,我想在更改此值时提交
amount_paid
。 For solution this, I create a function change that return me value. 为了解决这个问题,我创建了一个返回值的函数更改。 This value I need to submit.
我需要提交的这个值。 I tried this code:
我试过这段代码:
Template code: 模板代码:
<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>
Ts code: 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);
}
Please I have 2 situation, 1, I submit value input, ex, 100. This work correctly. 请我有2个情况,1,我提交值输入,ex,100。这项工作正常。 and 2. I submit input value when I change this value, so, from 100 I want to change 90, and this 90 I want to submit.
2.当我更改此值时,我提交输入值,因此,从100我想要更改90,而这90我想提交。 My function submit also this first value
sale.amount_paid = this.amountpaid;
我的功能也提交了这个第一个值
sale.amount_paid = this.amountpaid;
UPDATE: demo example for my problem 更新:我的问题的演示示例
The problem is, amount_paid field should be modified, and the modified value should be preserved. 问题是,应修改amount_paid字段,并保留修改后的值。 How to modify my value?
如何修改我的价值? I want the field two binding, if I can't change, submit
get amountpaid() {}
otherwise submit my value input. 我希望字段有两个绑定,如果我无法更改,请提交
get amountpaid() {}
否则提交我的值输入。
Image 1 I save the form without changing the value. 图1我保存表单而不更改值。
Image 2 I save the form with the changed value, but the value doesn't change.
图2我使用更改的值保存表单,但值不会更改。 show in console.
在控制台中显示。
Two things: 两件事情:
<input formControlName="amount_paid" id="amount_paid" type="number" class="validate">
And in .ts: 在.ts:
this.addsale.controls['amount_paid'].valueChanges.subscribe(value => { amount_paid is in value and it's called every time the value changes. });
or for the whole form: 或整个表格:
this.addsale.valueChanges.subscribe(value => {
// whole form object is in value and it's called every time any form field changes.
});
Edit based on the comments and your example: Here is the working solution . 根据评论和您的示例进行编辑:这是可行的解决方案 。 At least I hope I understand you correctly this time.
至少我希望这次我能正确理解你。
Why you do not use the binding : 为什么不使用绑定:
<input id="amount_paid" [(ngModel)]="amountpaid" type="number" class="validate" (change)="updateForm($event)">
Typescript : 打字稿:
amountpaid:string = '';
updateForm(ev: any) {
console.log(this.amountpaid);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.