简体   繁体   English

Angular 反应式表单在生产构建的验证条件上显示错误

[英]Angular Reactive form showing error on validation condition on production build

Using reactive forms in Ionic application showing error on production build.在 Ionic 应用程序中使用反应式 forms 显示生产构建错误。

67: This condition will always return 'false' since the types 'string' and 'number' have no overlap. 67:此条件将始终返回 'false',因为类型 'string' 和 'number' 没有重叠。

[disabled]="(schedule == 2 && getAboutControl.schedule_time.errors?.required ||
 getAboutControl.schedule_date.errors?.required)"

my Form init:我的表单初始化:

initAboutForm() {
    this.aboutForm = this.formBuilder.group({
      is_schedule: ['1'], // will depend on this to make date and time required.
      schedule_date: [''],
      schedule_time: [''],
      // other values
    })
}

my Html:我的 Html:

    <ion-item>
      <ion-label>Date:</ion-label>
      <ion-datetime displayFormat="DD MMM, YYYY" min="2021" max="2030" formControlName="schedule_date">
       </ion-datetime>
    </ion-item>
    <div class="text-danger">
       <div *ngIf="getAboutControl.schedule_date.touched && getAboutControl.schedule_date.errors?.required">
        Date is required *
       </div>
    </div>

   <ion-item>
     <ion-label>Time:</ion-label>
     <ion-datetime displayFormat="HH:mm" formControlName="schedule_time"></ion-datetime>
   </ion-item>
   <div class="text-danger">
     <div *ngIf="getAboutControl.schedule_time.touched && getAboutControl.schedule_time.errors?.required">
      Time is required *
     </div>
   </div>

As mentioned in the comments, in the template you're comparing schedule (string) against 2 (number).如评论中所述,在模板中,您将schedule (字符串)与2 (数字)进行比较。

So the fix would be to compare schedule against '2' instead:因此,解决方法是将schedule'2'进行比较:

[disabled]="(schedule === '2' && getAboutControl.schedule_time.errors?.required || getAboutControl.schedule_date.errors?.required)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM