根据Angular 4中下拉列表中的值更改日期

问题描述 投票:1回答:1

我有这个付款期限是下拉列表。而且付款期限有价值。所以我需要从当前日期添加这些值,以便我可以显示它。这是我下面的代码。

this.addForm = this.fb.group({
      payment_term: new FormControl(null, Validators.required),
      due_date: new FormControl(null, Validators.required),
});



onSelectPaymentTerm(event){
    console.log(event)
    this.addForm.patchValue({
      due_date: this.date.setDate( this.date.getDate() + event)
    })
  }

HTML

<div class="form-group row">
    <label class="col-sm-3 col-form-label">Payment Term</label>
    <div class="col-sm-9">
        <select type="text" class="form-control" formControlName="payment_term" (ngModelChange)="onSelectPaymentTerm($event)">
            <option value="null" hidden>-- Select Payment Term --</option>
            <option *ngFor="let payment of payments" [value]="payment?.value">{{ payment?.name }}</option>
        </select>
    </div>
</div>

<div class="form-group row">
    <label class="col-sm-4 col-form-label">Due Date</label>
    <div class="col-sm-8">
        <input type="text" class="form-control" formControlName="due_date" readonly>
    </div>
</div>
angular angular-reactive-forms angular4-forms
1个回答
1
投票

看看下面的内容 - 使用Vanilla JS

Demo

更清洁的方法 - 使用moment.js

Demo

  1. 通过反复使用反应形式
  2. 无需使用(ngModelChange)="onSelectPaymentTerm($event)"。但这不是问题
  3. 选定的值以字符串形式出现,因此在添加到日期之前必须使用parseInt转换为整数
  4. 首选Moment.js在Javascript中进行日期操作
© www.soinside.com 2019 - 2024. All rights reserved.