如何将金额从输入字段绑定到按钮



我正在尝试将总弹药量绑定到html表单上的支付按钮。我在.ts文件中创建了计算总弹药量的函数。为了验证我的表单,我使用了FormGroup和formBuilder。这是我的打字脚本:

import { Component, OnInit, OnChanges } from '@angular/core';
@Component({
selector: 'app-payment',
templateUrl: './payment.component.html',
styleUrls: ['./payment.component.css']
})
export class PaymentComponent implements OnInit {
paymentForm: FormGroup;
totalAmount: number;
constructor() { }
constructor(private formBuilder: FormBuilder, private appService: AppService) { }
ngOnInit() {
this.paymentForm = this.formBuilder.group({
'tenName': ['', Validators.required],
'appAddress': ['', Validators.required],
'textArea': ['', Validators.required],
'phoneNumber': ['', [Validators.required, Validators.maxLength(11)]],
'emailAddress': ['', [Validators.required, Validators.email, Validators.pattern('[^@]*@[^@]*')]]
});
}
OnChanges() {
}
calculatePayment() {
this.totalAmount = (this.paymentForm.value.amount * 0.0375) + this.paymentForm.value.amount;
}
}

在我的html表单中,我有一堆带有姓名地址等的输入字段。我想从我的输入字段"您正在支付的弹药数量"中获取值,并计算总数并显示在支付按钮中。这是html部分:

<form>
<div class="row row-padding padding-content md-padding-content lg-padding-content">
<div class="col-12"></div>
<div class="panel panel-info">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6  panel-heading">
<div class= "col-xs-12 col-sm-12 col-md-6 col-lg-6"><span><i class="glyphicon glyphicon-lock"></i></span> Secure Payment
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 pull-right"><img class="pull-right cards-img" src="assets/img/credit-cards.png">
</div>
</div>
<div class="panel-body panel-box">
<div class="form-group">
<div class="col-sm-12 col-md-12 col-lg-12">
<strong>Amount You Are Paying</strong>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<input type="text" class="form-control" name="pay_amount" id="pay_amount" value="" (input)="calculatePayment()" />
<span class="afix">***3.75% fee will be added to the amount (see below)</span>
</div>
</div>
<div class="form-group">
<div class="col-xs-12 col-md-12 col-sm-12 col-xs-12 col-lg-12 pull-right btn-row">
<button type="submit"  class="btn btn-primary btn-lg btn-submit-fix pull-right">Pay {{totalAmount}}</button>
</div>
</div>
</div>
</div>
</div>
</form>
<form>
<div class="row row-padding padding-content md-padding-content lg-padding-content" [formGroup]="paymentForm">
<div class="col-12"></div>
<div class="panel panel-info">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6  panel-heading">
<div class= "col-xs-12 col-sm-12 col-md-6 col-lg-6"><span><i class="glyphicon glyphicon-lock"></i></span> Secure Payment
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 pull-right"><img class="pull-right cards-img" src="assets/img/credit-cards.png">
</div>
</div>
<div class="panel-body panel-box">
<div class="form-group">
<div class="col-sm-12 col-md-12 col-lg-12">
<strong>Amount You Are Paying</strong>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<input type="text" class="form-control"  formControlName="payAmt" (input)=calculatePayment()/>
<span class="afix">***3.75% fee will be added to the amount (see below)</span>
</div>
</div>
<div class="form-group">
<div class="col-xs-12 col-md-12 col-sm-12 col-xs-12 col-lg-12 pull-right btn-row">
<button type="submit"  class="btn btn-primary btn-lg btn-submit-fix pull-right">Pay {{totalAmount}}</button>
</div>
</div>
</div>
</div>
</div>
</form>
@Component({
selector: 'app-payment',
templateUrl: './payment.component.html',
styleUrls: ['./payment.component.css']
})
export class PaymentComponent implements OnInit {
paymentForm: FormGroup;
totalAmount: number;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.paymentForm = this.formBuilder.group({
'tenName': ['', Validators.required],
'appAddress': ['', Validators.required],
'textArea': ['', Validators.required],
'phoneNumber': ['', [Validators.required, Validators.maxLength(11)]],
'payAmt': ['', Validators.required],
'emailAddress': ['', [Validators.required, Validators.email, Validators.pattern('[^@]*@[^@]*')]]
});
}
OnChanges() {
}
calculatePayment() {
this.totalAmount = +this.paymentForm.value.payAmt + (+this.paymentForm.value.payAmt * 0.0375);
}
}

最新更新