【问题标题】:How to set null for date form field in reactive form in angular 6如何在角度6中以反应形式为日期表单字段设置null
【发布时间】:2020-04-11 19:57:29
【问题描述】:

如何为反应形式提交的日期设置 null。我试过但没有工作。任何知道的人请帮助找到解决方案。

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));

【问题讨论】:

标签: angular6 angular7 angular-reactive-forms


【解决方案1】:

把你的input改成这样

&lt;input type="text" class="form-control" formControlName="publishdate" ngbDatepicker #d="ngbDatepicker"&gt;

所以,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));
  }
}

工作示例: https://stackblitz.com/edit/ngb-datepicker-validation-opt-out-vk6maq

【讨论】:

  • 你能回答这个问题吗?我在表格中有很多文件。但是我如何重置特定字段? obj = ['电子邮件','密码','确认密码']; for(var i=0;i
  • @NilaVaani,请创建 stackblitz 示例并描述您的问题,我会尽力解决..
  • 请检查resetfn()
  • @NilaVaani,我会调查一下,您的预期结果是什么?
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 2020-11-21
  • 2019-02-14
  • 2018-12-28
  • 2018-01-01
  • 2022-01-15
  • 2020-05-01
  • 2019-10-21
相关资源
最近更新 更多