角度2-6日期格式字段,防止清除

时间:2018-08-24 14:03:50

标签: angular forms angular-material angular-material2

我有一个预填的日期输入:

enter image description here

它已经预先填充,并且在所有潜在用例中都会被更改,但绝不会被清空。

我想防止它被清空。

这是我的html代码:

<form class="details" [formGroup]="forcastForm" [style.fontSize.px]="13">
  <div class="row no-gutters">
    <div class=".col-lg-8 .col-xl-12">
      <mat-form-field class="my-date-input">
        <mat-label i18n="confirmed date">Date</mat-label>
        <input matInput type="date" formControlName="dateFC">
      </mat-form-field>
    </div>
  </div>
</form>

和我的打字稿:

forcastForm: FormGroup;

constructor(
  private fb: FormBuilder,
  private datePipe: DatePipe)
  {
    const j = new Date;
    let month = (j.getUTCMonth() + 1).toString();
    if(month.length < 2) month = '0' + month;
    const dateCode = j.getUTCFullYear().toString() + month + j.getUTCDate().toString();
    const dt = this.datePipe.transform(j, 'yyyy-MM-dd');

    this.forcastForm = fb.group({
      savedId: new FormControl({value: dateCode, disabled: false}, Validators.required),
      dateFC: new FormControl({value: dt, disabled: false}, Validators.required),
      comentary: new FormControl({value: '', disabled: false}, Validators.required),
    });

  }

我可以通过/deep/ CSS隐藏 x 图标,但是没有真正的方法在代码检查器中查看 x 的代码

1 个答案:

答案 0 :(得分:1)

required属性添加到输入元素:

<input matInput type="date" required formControlName="dateFC">