2017-09-16 71 views
0

我有一个角度2应用程序,我需要(除了其他人),这4个字段:多个相关的字段角度2

毛额,折扣,折扣百分比和净额。 的公式为:

净额=总金额-折扣

(如果折扣百分比被提供时,它应该计算折扣和上述式使用)。

有没有办法让我可以让这4件事情相互依赖,如果其中一个改变,根据公式改变?

+0

可以使用ngIf –

+0

@BhojendraNepal怎么样? – rahulserver

回答

1

你可以写这样的事情:

import {Component} from "@angular/core"; 
@Component({ 
    selector: 'app-root', 
    template: ` 
     Gross: <input type='text' [(ngModel)]="grossAmount"> 
     Discount Percent <input type='text' [(ngModel)]="discountPercent" (change)="calculateNet()"> 
     Discount: {{discount}} 
     Net Amount: {{netAmount}} 
     ` 
}) 
export class AppComponent { 
    grossAmount = 0; 
    discount = 0; 
    discountPercent=0; 
    netAmount=0; 

calculateNet(){ 
    if (this.discountPercent > 0){ 
    this.discount = this.grossAmount*this.discountPercent/100; 
    this.netAmount = this.grossAmount - this.discount; 
    } 
} 
} 
+0

谢谢你。我最终使用了输入标签的所有4个属性的变化事件处理程序(也包括一个单独的变化事件处理程序的div中的输入标签工作正常) – rahulserver