首页 > 解决方案 > Angular 6:正确实现 ControlValueAccessor

问题描述

我正在使用 bootstrap 4 和 angular 6 以及 igx-calendar 为 angular 创建一个简单的预订表格,其中包含以下表格。igx 日历

HTML。

<form [formGroup]="angForm" class="form-element">
      <div class="col-sm-4 offset-sm-2 about-booking_calendar">
        <div class="form-group form-element_date">
        <app-calendar formControlName="date" #date></app-calendar>
        </div>
      </div>
      <div class="col-sm-4 about-booking_form">
        <div class="form-group form-element_email">
          <input type="email" class="form-control info" placeholder="Email" formControlName="email" #email (ngModelChange)="onChange($event)">
        </div>
        <div *ngIf="angForm.controls['email'].invalid && (angForm.controls['email'].dirty || angForm.controls['email'].touched)"
          class="alert alert-danger">
          <div *ngIf="angForm.controls['email'].errors.required">
            Email is required.
          </div>
        </div>
        <div class="input-group mb-3 form-element_city">
          <select class="custom-select" id="inputGroupSelect01" #cityName>
            <option selected *ngFor="let city of cities" [ngValue]="city.name">{{city.name}}</option>

          </select>
        </div>
        <div class="input-group mb-3 form-element_hotel">
          <select class="custom-select" id="inputGroupSelect01" #hotelName>
            <option selected *ngFor="let hotel of hotels" [ngValue]="hotel.name">{{hotel.name}}</option>

          </select>
        </div>
        <div class="form-group">
          <button type="submit" (click)="addReview(date.value, email.value, cityName.value , hotelName.value)" class="btn btn-primary btn-block form-element_btn"
            [disabled]="!validEmail">Book</button>
        </div>
      </div>
    </form>

日历组件.ts

import { Component, forwardRef, Input, OnInit, ElementRef, ViewChild } from '@angular/core';
import { IgxCalendarComponent, IgxDialogComponent } from 'igniteui-angular';
import { FormControl, ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';
@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.scss'],
  providers: [
    { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CalendarComponent), multi: true }
  ]
})
export class CalendarComponent implements ControlValueAccessor, OnInit {
  @ViewChild('calendar') public calendar: IgxCalendarComponent;
  @ViewChild('alert') public dialog: IgxDialogComponent;
  // tslint:disable-next-line:no-input-rename
  @Input('date') _date;
  @ViewChild('date') private elDate: ElementRef;
  instance;
  propagateChange: any = () => { };
  ngOnInit() {
    this.instance = this.calendar(this.elDate.nativeElement, {
      defaultDate: this.date,
      onChange: (selectedDates, dateStr, instance) => {
        this.date = selectedDates[0];
      }
    });
  }
  get date() {
    return this._date;
  }

  set date(val) {
    this._date = val;
    this.propagateChange(val);
  }

  writeValue(value) {
    if (value) {
      this.date = value;
      this.instance.setDate(this.date, true);
    }
  }

  registerOnChange(fn) {
    this.propagateChange = fn;
  }

  registerOnTouched() { }

  public verifyRange(dates: Date[]) {
    if (dates.length > 5) {
      this.calendar.selectDate(dates[0]);
      this.dialog.open();
    }
  }
}

注意:这是表格的视觉效果: 在此处输入图像描述

我在编译时在控制台上收到以下错误,问题首先出在这一行:

  ngOnInit() {
    this.instance = this.calendar(this.elDate.nativeElement, {
      defaultDate: this.date,
      onChange: (selectedDates, dateStr, instance) => {
        this.date = selectedDates[0];
      }
    });
  }

作为参考,我在此链接中使用了此方法: next.plnkr.co/edit/okIjPb6aUcrzx3t7edae?p=info&preview 他们使用 flatpickr 我使用 igx-calendar 来自:

错误:无法调用类型缺少调用签名的表达式。类型“IgxCalendarComponent”没有兼容的调用签名。

我只想让用户选择一个日期并填写电子邮件、城市、酒店等表格并能够提交表格。

我的代码有什么问题?请帮忙,我只是在学习所有这些东西。

标签: javascriptangularhtmltwitter-bootstraptypescript

解决方案


推荐阅读