angular - Angular 2 Bootstrap 日期选择器的输入未设置值

标签 angular typescript ng-bootstrap

我有一个输入,我想用 Angular 2 Bootstrap 日期选择器填充它。当页面打开时,使用 value="{{ getStartDate() | date:'fullDate'}}" 以今天的日期启动该值。但是,当我单击按钮并打开日期选择器并选择新日期时,该值不会填充输入。另外,我无法再再次单击按钮来关闭日期选择器。

HTML:

<form class="form-inline">
<div>
<div class="form-group" [ngClass]="{'has-error':!secondForm.controls['startDate'].valid && secondForm.controls['startDate'].touched}">
  <input value="{{ getStartDate() | date:'fullDate'}}" style="width:250px" class="form-control" type="text" [formControl]="secondForm.controls['startDate']">
</div>
<div style="display:inline-block">
  <ngb-datepicker *ngIf="startCheck==true;" [(ngModel)]="dt" class="dropdown-toggle" [ngModelOptions]="{standalone: true}" style="position:absolute; z-index:1"></ngb-datepicker>
</div>
<button type="button" class="btn icon-calendar" (click)="showDatePick()"></button>
<button type="button" class="btn icon-search" [disabled]="!secondForm.valid"></button>
 </div>
 </form>

typescript :

import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';

@Component({
selector: 'calendar-pick',
styleUrls: ['../app.component.css'],
templateUrl: './calendarpick.component.html'
})

export class CalendarPickComponent {
public dt:Date = new Date();
public startCheck: boolean = false;
//Might need to change this to complexForm, not sure yet
secondForm : FormGroup;

public constructor(fb: FormBuilder) {
this.secondForm = fb.group({
  'startDate' : [this.dt, Validators.required]
})
this.secondForm.valueChanges.subscribe( (form: any) => {
    console.log('form changed to:', form);
  }
);
}

public getStartDate():number {
return this.dt && this.dt.getTime() || new Date().getTime();
}

public showDatePick():void {
if (this.startCheck == false){
  this.startCheck = true;
} else {
  this.startCheck = false;
}
}
}

最佳答案

ng-bootstrap datepicker 的模型不是 Date() 对象,而是一个由 {month, day,year} 组成的 NgbDateStruct

为了获得所需的行为,代码如下所示:

import {DatePipe} from "@angular/common";

@Component({providers: [DatePipe]})

public constructor(fb: FormBuilder, private datePipe: DatePipe)
public dt: NgbDateStruct;
<...>
public getStartDate():number {
let timestamp = this.dt != null ? new Date(this.dt.year, this.dt.month-1, this.dt.day).getTime() : new Date().getTime();
this.secondForm.controls['startDate'].setValue(this.datePipe.transform(timestamp, 'fullDate'));
}

标记更改为: <input style="width:250px" [value]="getStartDate()" class="form-control" type="text" [formControl]="secondForm.controls['startDate']">

这是完整的骗子:https://plnkr.co/edit/zqGpoJZ1psKmST0S7Ix0?p=preview

请注意,ng-bootstrap 团队的月份索引是从 0 开始的,而不是从 1 开始,这在将原生 Date 对象与 NgbDate 结合使用时会让人抓狂。

此外,默认值 new Date().getTime() 的编写方式意味着您永远不会有可能需要/可能不需要的空白值。而且它总是肮脏而有效的。

关于angular - Angular 2 Bootstrap 日期选择器的输入未设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407232/

相关文章:

css - ItemSliding 不适用于按钮

javascript - Angular 2 : Multiple html lines of code(template) not rendering in browser

angular - ng-bootstrap 示例从组件调用方法

twitter-bootstrap - 为什么 Bootstrap Navbar 总是折叠?

typescript - 出现在自动生成的声明源文件中的三斜杠引用

angular - 为来自其他父组件的 div 提供动态 ID

node.js - 如何创建一个自定义管道来接收对象作为 Angular 4 中的过滤器

angular - aws-sdk:从 Angular 5 更新到 Angular 6 后崩溃

typeahead - 选择结果后如何清除预先输入的输入?

angular - 如何将 NgControl 状态传递给 Angular 中的子组件,实现 ControlValueAccessor?