簡體   English   中英

Angular 5將日期綁定到字段(反應形式)

[英]Angular 5 bind date to field (reactive forms)

輸入類型日期有問題。 我想綁定組件中的數據。 這是我的領域:

<div class="col-md-6">
    <label for="dateOfReport">Data zgłoszenia błędu:</label>
    <input type="date" formControlName="dateOfReport" id="dateOfReport" class="form-control" [value]="report.dateOfReport | date:'dd.MM.yyyy'"> {{report.dateOfReport | date:'dd.MM.yyyy'}}
  </div>

這是變量dateOfReport的外觀:

new Date().toJSON().slice(0, 10)

我哪里失敗了? {{..}}顯示良好的日期,但我的字段未將其設為默認日期。

使用反應式表單時,不應將數據直接綁定到控件,而應使用FormGroup的setValue()或patchValue()。

    <div class="col-md-6">
        <label for="dateOfReport">Data zgłoszenia błędu:</label>
        <input type="date" formControlName="dateOfReport" id="dateOfReport" class="form-control" [value]="report.dateOfReport | date:'dd.MM.yyyy'"> {{report.dateOfReport | date:'dd.MM.yyyy'}}
      </div>

In your TS file use ur formGroup Name and access the element. For Example if you have FormGroup name as myForm and you have the formcontrolname in it as dateOfReport you can set values to it like this.

    `this.myForm.patchValue({
    dateOfReport:'YOUR TIME VARIABLE HER'
    });`

You can either use setValue or patchValue in this case. Not that setValue wont fail silently. But patchValue will fail silent. It is recommended to use patchValue therefore

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM