Невозможно выполнить двустороннюю привязку в Mat-Datepicker - PullRequest
0 голосов
/ 16 февраля 2019

вот мой код ниже.Я использую mat-datepicker и настраиваю его на год, делая настройки в нем.Проблема заключается в том, что я реализую name="control" #control="ngModel" [(ngModel)]="", поэтому он выдает ошибку в консоли, но когда я удаляю name="control" #control="ngModel" [(ngModel)]="" из управления, чтобы он работал нормально.

    <input class="form-control" name="FDConversationDateYear" #FDConversationDateYear="ngModel" [(ngModel)]="objCompanyFinancialDetails.FDConversationDateYear" [matDatepicker]="dp" placeholder="Month and Year" [formControl]="date" required>

<mat-datepicker-toggle class="datepicker-toggler" matSuffix [for]="dp"></mat-datepicker-toggle>
                                    <mat-datepicker #dp
                                                    startView="multi-year"
                                                    (yearSelected)="chosenYearHandler($event, dp)"
                                                    panelClass="example-month-picker">
                                    </mat-datepicker>

Вот component.ts

import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import {MatDatepicker} from '@angular/material/datepicker';

// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import {default as _rollupMoment, Moment} from 'moment';

const moment = _rollupMoment || _moment;

// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = {
  parse: {
    dateInput: 'MM/YYYY',
  },
  display: {
    dateInput: 'YYYY',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'YYYY',
  },
};

/** @title Datepicker emulating a Year and month picker */
@Component({
  selector: 'datepicker-views-selection-example',
  templateUrl: 'datepicker-views-selection-example.html',
  styleUrls: ['datepicker-views-selection-example.css'],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},

    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})
export class DatepickerViewsSelectionExample {
  date = new FormControl(moment());

  chosenYearHandler(normalizedYear: Moment, datepicker: MatDatepicker<Moment>) {
    const ctrlValue = this.date.value;
    ctrlValue.year(normalizedYear.year());
    this.date.setValue(ctrlValue);
  }
}

и вот мои консольные ошибки

Here is my console error image

1 Ответ

0 голосов
/ 16 февраля 2019

Удалить атрибут formControl при использовании шаблонно-управляемых форм:

HTML-код:

<input class="form-control" name="FDConversationDateYear" #FDConversationDateYear="ngModel" [(ngModel)]="date" [matDatepicker]="dp" placeholder="Month and Year" required>
<mat-datepicker-toggle class="datepicker-toggler" matSuffix [for]="dp"></mat-datepicker-toggle>
<mat-datepicker #dp startView="multi-year" (yearSelected)="chosenYearHandler($event, dp)" panelClass="example-month-picker">
</mat-datepicker>

Код TS:

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

/** @title Datepicker selected value */
@Component({
  selector: 'datepicker-value-example',
  templateUrl: 'datepicker-value-example.html',
  styleUrls: ['datepicker-value-example.css'],
})
export class DatepickerValueExample {
  date = new Date();
}

StackBlitz

...