Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular ngx日期选择器:设置前几年的初始年份值_Angular - Fatal编程技术网

Angular ngx日期选择器:设置前几年的初始年份值

Angular ngx日期选择器:设置前几年的初始年份值,angular,Angular,我在Angular 8中使用ngx datepicker,我想将年份(日历)从今天往后设置18年。 我用的是这里的例子 此示例设置minDate和maxDate,如何设置初始日期/年份,即打开下拉列表时显示2002年。尝试如下 组件。ts import { Component } from '@angular/core'; @Component({ selector: 'demo-datepicker-min-max', templateUrl: './min-max.componen

我在Angular 8中使用ngx datepicker,我想将年份(日历)从今天往后设置18年。 我用的是这里的例子

此示例设置minDate和maxDate,如何设置初始日期/年份,即打开下拉列表时显示2002年。

尝试如下

组件。ts

import { Component } from '@angular/core';

@Component({
  selector: 'demo-datepicker-min-max',
  templateUrl: './min-max.component.html'
})
export class DemoDatepickerMinMaxComponent {
  minDate: Date;
  maxDate: Date;
  currentData: Date;

  constructor() {
     this.minDate = new Date();
     this.maxDate = new Date();
     this.currentData = new Date();
     this.currentData.setFullYear(this.minDate.getFullYear() -3);
     this.minDate.setFullYear(this.minDate.getFullYear() -3);
     console.log(this.minDate);
     this.maxDate.setDate(this.maxDate.getDate() + 7);
  }
}
component.html

<div class="row">
  <div class="col-xs-12 col-12 col-sm-6 col-md-4 form-group">
    <input class="form-control"
           placeholder="Datepicker"
           [(ngModel)]="currentData"
           bsDatepicker
           [minDate]="minDate"
           [maxDate]="maxDate">
  </div>
</div>

我已根据文档进行了更改

改变你的html格式

<div class="row">
    <div class="col-xs-12 col-12 col-sm-6 col-md-4 form-group">
        <input class="form-control"
           placeholder="Datepicker"
           bsDatepicker
           [bsValue]="bsValue"
           [minDate]="minDate"
           [maxDate]="maxDate">
  </div>
</div>
这是输出 希望这对你有帮助

谢谢

这是stackblitz的工作
import { Component } from "@angular/core";
@Component({
  selector: "demo-datepicker-min-max",
  templateUrl: "./min-max.component.html"
})
export class DemoDatepickerMinMaxComponent {
  minDate: Date;
  maxDate: Date;
  bsValue = new Date();
  constructor() {
    this.minDate = new Date();
    this.maxDate = new Date();
    this.minDate.setFullYear(this.minDate.getFullYear() - 18);
    console.log(this.minDate);
    this.maxDate.setDate(this.maxDate.getDate() + 7);
    this.bsValue = new Date(
      new Date().setFullYear(new Date().getFullYear() - 18)
    );
  }
}