简体   繁体   English

为什么我不能用计算值更新 Angular 反应式表单字段的值?

[英]Why I can't update the value of an Angular reactive form field with a calculated value?

I am working on an Angular application using reactive form.我正在使用反应形式开发 Angular 应用程序。 Into the HTML code of a component I have this field that have to contains a value that is calculated when an operation is performed (changing another value in my frontend).在组件的 HTML 代码中,我有这个字段必须包含一个在执行操作时计算的值(在我的前端更改另一个值)。

<div class="row">
  <div class="col-2">
    <p>Valore attuale asset</p>
  </div>
  <div class="col-10">
    <p-inputNumber id="deductible" formControlName="current_asset_value"
                   mode="currency" currency="EUR" locale="de-DE"
                   [disabled]="true"> 
    </p-inputNumber>                         
  </div>
</div>

As you can see it show the value of the current_asset_value formControlName.如您所见,它显示了current_asset_value formControlName 的值。

Then into the TypeScript code of this component I have defined the form into my ngOnInit() method:然后进入这个组件的 TypeScript 代码,我在我的ngOnInit()方法中定义了表单:

this.assetDetailsForm = this.fb.group({
    asset_type: [this.assetDetailsSelected.asset_type, [Validators.required]],
    asset_model: [this.assetDetailsSelected.asset_model, [Validators.required]],
    guarantee_start_date: [this.assetDetailsSelected.guarantee_start_date, []],
    guarantee_duration: [2, []],
    guarantee_end_date: [null, []],
    asset_action: [this.assetDetailsSelected.asset_action, []],
    allocation: [null, []],
    asset_features: [this.assetDetailsSelected.asset_features, [Validators.required]],
    serial_number: [this.assetDetailsSelected.serial_number, [Validators.required]],
    accessories: [this.assetDetailsSelected.accessories, [Validators.required]],
    allocation_date: [this.assetDetailsSelected.allocation_date, [Validators.required]],
    company: [this.assetDetailsSelected.company, [Validators.required]],
    notes: [this.assetDetailsSelected.notes, [Validators.required]],
    invoice: [this.assetDetailsSelected.invoice, [Validators.required]],
    invoice_cost: [this.assetDetailsSelected.invoice_cost, [Validators.required]],
    deductible: [this.assetDetailsSelected.deductible, [Validators.required]],
    current_asset_value: [null, []],
    status: [this.assetDetailsSelected.status, []]
});

As you can see this field value is actually null and it is ok:如您所见,该字段值实际上是 null 并且可以:

current_asset_value: [null, []],

Then, when the user perform an operation on this form (when the value of the invoice_cost or of the deductible fields changes) is performed this component method that set the value of the current_asset_value form field.然后,当用户在此表单上执行操作时(当invoice_costdeductible字段的值发生变化时),执行设置current_asset_value表单字段值的组件方法。 I have done it in this way:我是这样做的:

public calculateAssetDepractionValue() {
    let today:Date =  new Date();
    console.log("TODAY DATE: ", today);
    console.log("GUARANTEE START DATE: ", this.assetDetailsSelected.guarantee_start_date);

    let monthsFromPurchase:number =  this.monthDiff(today, this.assetDetailsSelected.guarantee_start_date);
    console.log("monthsFromPurchase: " + monthsFromPurchase);

    let monthlyDevaluation = (this.assetDetailsSelected.invoice_cost - this.deductibleNg) / 36;
    let actualValue = this.assetDetailsSelected.invoice_cost - (monthlyDevaluation * monthsFromPurchase);

    console.log("actualValue: " + actualValue);

    this.assetDetailsForm.value.current_asset_value = actualValue;
}

This method is correctly executed (I can see it using the dubugger and the console.log() output) but the set value is not updated in my fronted into the form.此方法已正确执行(我可以使用 dubugger 和 console.log() 输出看到它)但设置值未在我的表单中更新。 This is what I am obtaining:这就是我得到的:

在此处输入图像描述

As you can see when I change the value of the highligted form field the calculateAssetDepractionValue() method is performed and the actualValue is calculated, finnally it executed this line to set/update the value of the current_asset_value form field:正如您所看到的,当我更改 highligted 表单字段的值时,执行了calculateAssetDepractionValue()方法并计算了actualValue ,最后它执行了这一行来设置/更新current_asset_value表单字段的值:

this.assetDetailsForm.value.current_asset_value = actualValue;

but as you can see in the print screen the value of the current_asset_value is not rendered into my view.但正如您在打印屏幕中看到的那样, current_asset_value的值并未呈现到我的视图中。

Why?为什么? What is wrong?怎么了? What am I missing?我错过了什么? How can I try to fix this issue?我该如何尝试解决此问题?

Try using any of the below function approaches.尝试使用以下任何 function 方法。

this.assetDetailsForm.get('current_asset_value').setValue(actualValue);

this.assetDetailsForm.controls.current_asset_value.setValue(actualValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么由datetimepicker设置时,我在Angular 4中的反应式表单不更新值 - Why doesn't my Reactive Form in Angular 4 update the value when set by datetimepicker 我无法以角电抗形式捕获输入字段的值 - I am not able to catch the value of the input field in a angular reactive form 如何在Angular反应表单上动态创建和修补选择字段的值? - How can I dynamically create and patch the value of a select field on an Angular reactive form? 在 Angular 中以反应形式更改隐藏字段的值 - Changing the value of a hidden field in a reactive form in Angular 反应性 Forms 不更新表单字段中的值 Angular 7 - Reactive Forms Not Updating Value In Form Field Angular 7 Angular 9 反应式表单无法更新值 - Angular 9 Reactive Form failing to update value Angular ViewChildren 无法获取更改的反应形式值 - Angular ViewChildren can't get changed reactive form value angular 反应形式中字段形式值的前缀值 - Prefix value to a field form value in angular reactive form 基于其他表单控件更新“禁用角反应性表单”字段的值 - Update Value of Angular Reactive Form disabled field based on other Form Controls 角度2/4 —将单选按钮[value]绑定到对象或函数时,为什么此反应式表单值不更新? - Angular 2/4 — When binding a radio button [value] to an object or function, why does this reactive form value not update?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM