Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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
Javascript 如何在Angular中创建月份范围选择器_Javascript_Angular_Typescript_Monthcalendar - Fatal编程技术网

Javascript 如何在Angular中创建月份范围选择器

Javascript 如何在Angular中创建月份范围选择器,javascript,angular,typescript,monthcalendar,Javascript,Angular,Typescript,Monthcalendar,我在stackoverflow上遇到了许多类似的问题。但请相信我,我至少实现了十个解决方案,但我没有得到正确的结果,而且大多数解决方案都在使用jquery或javascript,而我必须使用typescript。我正在创建一个简单的月范围选择器,没有其他火箭科学功能。在一个接一个地实现了一个解决方案之后,我的实际代码变得非常脏。所以我在stackblitz上创建了一个新的最小项目。另外,在进行任何更改之前,我会给出我的代码 Mymonthpicker.component.html: <di

我在stackoverflow上遇到了许多类似的问题。但请相信我,我至少实现了十个解决方案,但我没有得到正确的结果,而且大多数解决方案都在使用
jquery
javascript
,而我必须使用
typescript
。我正在创建一个简单的月范围选择器,没有其他火箭科学功能。在一个接一个地实现了一个解决方案之后,我的实际代码变得非常脏。所以我在stackblitz上创建了一个新的最小项目。另外,在进行任何更改之前,我会给出我的代码

Mymonthpicker.component.html

<div class="dropdown">
  <span>
    <input  placeholder="{{sampletext}}, {{inputText}}">
  </span>
  <div class="my-table-div dropdown-content">
    <div class="year-div">
      <input type="number" value="2018" min="2018" max="2024" [(ngModel)]="inputText">
    </div>
    <hr>
    <div *ngFor="let row of matrix" class="my-table">
      <span *ngFor="let x of row">
        <span class="month-name" (click)="onClick(x)">{{ x }}</span>
      </span>
    </div>
  </div>
</div>
请提出解决办法


附言:我不想使用任何第三方图书馆。甚至连引导都没有。我必须从头开始。

您只需使用两个选择菜单或自动完成来选择月份和年份。例如,您可以使用以下简单代码:

<form [formGroup]="MonthYearFormGroup">
  <h4>Select a month here:</h4>
  <mat-form-field>
    <mat-label>Month</mat-label>
    <mat-select formControlName="selected_month">
      <mat-option *ngFor="let month of monthList" [value]="month.value">
        {{month.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>

  <h4>Select a year here:</h4>
  <mat-form-field>
    <mat-label>Year</mat-label>
    <mat-select formControlName="selected_year">
      <mat-option *ngFor="let year of yearList" [value]="year.value">
        {{year.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>
  <button (click)="save()">Save Selected Date</button>
</form>

我没有得到你的确切答案requirement@RajSan我要在angular中创建一个月范围选择器。是否要使用1个输入字段创建月范围选择器?是否要两个月,从和到年?@RajSan.Yes。确切地但我会在一年后从其他部分处理。现在就让它硬编码吧。现在我想在同一文本字段中显示该范围。请看我的stackblitz我刚做了一些改变。非常感谢您的帮助。:-)我实现了这个解决方案。但我有一个错误。你能看看这个吗:
<form [formGroup]="MonthYearFormGroup">
  <h4>Select a month here:</h4>
  <mat-form-field>
    <mat-label>Month</mat-label>
    <mat-select formControlName="selected_month">
      <mat-option *ngFor="let month of monthList" [value]="month.value">
        {{month.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>

  <h4>Select a year here:</h4>
  <mat-form-field>
    <mat-label>Year</mat-label>
    <mat-select formControlName="selected_year">
      <mat-option *ngFor="let year of yearList" [value]="year.value">
        {{year.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>
  <button (click)="save()">Save Selected Date</button>
</form>
import {Component} from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

export interface MonthYear {
  value: string;
  viewValue: string;
}

@Component({
  selector: 'select-overview-example',
  templateUrl: 'select-overview-example.html',
  styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample {

  public MonthYearFormGroup: FormGroup();


 yearList: MonthYear[] = Array(50).fill(1).map( (_, index) => { return { value: String(index + 2019), viewValue: String(index + 2019) } })

  monthList: MonthYear[] = [
    {value: '1', viewValue: 'Jan'},
    {value: '2', viewValue: 'Feb'},
    {value: '3', viewValue: 'Mar'},
    {value: '4', viewValue: 'Apr'},
    {value: '5', viewValue: 'May'},
    {value: '6', viewValue: 'Jun'},
    {value: '7', viewValue: 'Jul'},
    {value: '8', viewValue: 'Aug'},
    {value: '9', viewValue: 'Sep'},
    {value: '10', viewValue: 'Oct'},
    {value: '11', viewValue: 'Nov'},
    {value: '12', viewValue: 'Dec'},
  ];

  constructor(
    private _formBuilder: FormBuilder,
  ) { }

  ngOnInit() {
    this.MonthYearFormGroup= this._formBuilder.group({
      selected_month: [{ value: '', required: true }],
      selected_year: [{ value: '', required: true }],
  });

  save() {
   //Your process here...
   console.log(this.MonthYearFormGroup.value.selected_month);
   console.log(this.MonthYearFormGroup.value.selected_year);
  }
}