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

时间:2018-03-02 06:59:16

标签: angular angular-reactive-forms angular4-forms

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

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>

1 个答案:

答案 0 :(得分:1)

  

使用Vanilla JS

查看以下内容

Demo

  

清洁方法 - 使用moment.js

Demo

  

解决方案

  1. 通过
  2. 使用反应表单
  3. 无需使用package main import ( "image" "image/color" "github.com/llgcode/draw2d" "github.com/llgcode/draw2d/draw2dimg" "github.com/llgcode/draw2d/draw2dpdf" "github.com/llgcode/draw2d/draw2dsvg" ) func coreDraw(gc draw2d.GraphicContext) { // Set some properties gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff}) gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff}) gc.SetLineWidth(5) // Draw a closed shape gc.BeginPath() // Initialize a new path gc.MoveTo(10, 10) // Move to a position to start the new path gc.LineTo(100, 50) gc.QuadCurveTo(100, 10, 10, 10) gc.Close() gc.FillStroke() } func main() { format := "svg" switch format { case "png": dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0)) gc := draw2dimg.NewGraphicContext(dest) coreDraw(gc) draw2dimg.SaveToPngFile("hello.png", dest) case "pdf": dest := draw2dpdf.NewPdf("L", "mm", "A4") gc := draw2dpdf.NewGraphicContext(dest) coreDraw(gc) draw2dpdf.SaveToPdfFile("hello.pdf", dest) case "svg": img := draw2dsvg.NewSvg() gc := draw2dsvg.NewGraphicContext(img) coreDraw(gc) draw2dsvg.SaveToSvgFile("hello.svg", img) } } 。但这不是问题
  4. 所选值以字符串形式出现,因此在添加到日期之前必须使用parseInt转换为整数
  5. 首选Moment.js在Javascript中进行日期操作