【问题标题】:How to bind amount from input field to the button?如何将输入字段中的金额绑定到按钮?
【发布时间】:2019-01-10 09:06:36
【问题描述】:

我正在尝试将总金额绑定到 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>

【问题讨论】:

    标签: html typescript angular5 angular2-forms


    【解决方案1】:
        <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);
          }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-18
      • 2018-08-03
      • 2020-02-11
      • 2018-05-08
      • 1970-01-01
      • 2023-03-04
      • 2019-01-15
      相关资源
      最近更新 更多