javascript - 乘法两个输入 Angular

标签 javascript angular angular4-forms

我想从 2 个输入(NUM_PRECIO_UNITARIONUM_PORCENTAJE_IVA)获取值,并在其他输入(NUM_VALOR_IVA)中显示乘法结果实际上这是我的 html

文件.html

<div class="form-group col-sm-5">
    <mat-form-field>                    
        <input matInput type="number" formControlName="NUM_VALOR_IVA" placeholder="Valor IVA" value="{{formDetalleOC.get('NUM_PRECIO_UNITARIO').value * formDetalleOC.get('NUM_PORCENTAJE_IVA').value}}" required>                  
    </mat-form-field>
</div>  

文件.ts

ngOnInit() {
    this.createControlItemOC
} 

createControlItemOC() {  
    this.formDetalleOC = this.fb.group({         
        NUM_PRECIO_UNITARIO: 0,
        NUM_PORCENTAJE_IVA: 0,   
        NUM_VALOR_IVA: 0,
    })  
}

这是前面的结果

enter image description here

但在我的表单中,值没有改变

enter image description here

最佳答案

在函数中创建以乘以组件类中的这些值

calcResult() {
    const o = this.formDetalleOC.get('NUM_PRECIO_UNITARIO').value * this.formDetalleOC.get('NUM_PORCENTAJE_IVA').value; 
    this.formDetalleOC.patchValue({ NUM_VALOR_IVA: o });
    return o;
}

在 HTML 模板中,调用该函数:

<div class="form-group col-sm-5">
    <mat-form-field>                    
        <input matInput type="number" formControlName="NUM_VALOR_IVA" placeholder="Valor IVA" [value]="calcResult()" required>                  
    </mat-form-field>
</div>

PS:如果您正在使用 FormBuilder,请不要将 ngModel 用于您的表单输入(您可以将它用于同一页面上的其他内容)。

关于javascript - 乘法两个输入 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47947142/

相关文章:

javascript - 从父数组中获取长度最大的数组

javascript - 学习backbone.js。我是否正确处理集合?

Javascript替换/修改变量指向的对象?

css - Angular 6 主题 : How do i set background color and use in entire app

angular - 如何在 Angular 2 中以长格式观察几个字段的变化

javascript - 你如何将 HTML 插入到 QuillJS 中?

Angular 2如何从输入中删除粘贴的内容

javascript - 如何使用 httpClient 从 Angular 中的可观察对象访问数据?

Angular,如何为两个字段中的任何一个设置验证应在 react 形式方法中进行验证

angular4-forms - Angular 4 与 Material 2 的 react 形式,禁用自动完成