0

我有两个 div,一个包含一个日期选择器/日历,允许用户选择他们想要的日期,第二个包含带有电子邮件、citym 和酒店的表单,

所以用户选择一个日期并填写表格并通过点击书提交。

这是我所拥有的

<div class="container about-booking-input">
  <div class="row about-title">
    <div class="col-md-7 about-title_bookingtitle">
      <h2>Booking</h2>
    </div>
  </div>
  <div class="row about-booking">
    <div class="col-sm-4 offset-sm-2 about-booking_calendar">
        <app-calendar></app-calendar>
    </div>
    <div class="col-sm-4 about-booking_form">
      <flash-messages></flash-messages>
      <form [formGroup]="angForm" class="form-element">
        <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(email.value, cityName.value , hotelName.value)" class="btn btn-primary btn-block form-element_btn"
            [disabled]="!validEmail">Book</button>
        </div>
      </form>

    </div>
  </div>
</div>

视觉上看起来像这样。 在此处输入图像描述

现在我可以只提交没有日期的表格,在谷歌搜索和谷歌搜索后发现没有任何帮助,

注意:我正在使用这个 datepicker ui:ignite -ui angular https://www.infragistics.com/products/ignite-ui-angular/angular/components/calendar.html

问题

如何将我的日历与表单集成并能够提交?

4

1 回答 1

2

您必须使您的app-calender组件成为自定义表单控件。关于此的一篇不错的文章:https ://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html

简而言之,您AppCalenderComponent必须实现ControlValueAccessor接口才能与formGroup. 在你成功地ControlValueAccessor为你AppCalenderComponent的 .

<form [formGroup]="form">
  <app-calendar formControlName="nameOfTheDatePickerControl"></app-calendar>
</form>
于 2018-07-06T02:36:13.507 回答