如何在 angular 6 中以反应形式为日期表单字段设置 null

How to set null for date form field in reactive form in angular 6

如何在反应式中为日期字段设置 null form.I 尝试过但没有 working.Anyone 知道请帮助找到解决方案。

app.component.html:

<input type="text" class="form-control" formControlName="publishdate" ngbDatepicker #datepicker="ngbDatepicker">

app.component.ts:

this.registerForm = this.fb.group({}); 
this.registerForm.addControl('publishdate', new FormControl('', null));

改变你的 input 成为

<input type="text" class="form-control" formControlName="publishdate" ngbDatepicker #d="ngbDatepicker">

因此,HTML 应该是这样的,

<form class="form-inline" [formGroup]="registerForm">
 <div class="form-group">
   <div class="input-group">
     <input type="text" class="form-control" formControlName="publishdate" ngbDatepicker #d="ngbDatepicker">
     <div class="input-group-append">
       <button class="btn btn-outline-secondary calendar" (click)="d.toggle()" type="button">
       </button>
     </div>
    </div>
   </div>
 </form>

TS,

import {Component, OnInit} from '@angular/core';
import { FormControl, FormGroup, FormBuilder, ValidationErrors } from "@angular/forms";

@Component({
  selector: 'ngbd-datepicker-popup',
  templateUrl: './datepicker-popup.html'
})
export class NgbdDatepickerPopup implements OnInit {

  registerForm: FormGroup;

  constructor(private fb: FormBuilder){}

  ngOnInit(){
    this.registerForm = this.fb.group({});
    this.registerForm.addControl('publishdate', new FormControl('', null));
  }
}

Working Example: https://stackblitz.com/edit/ngb-datepicker-validation-opt-out-vk6maq