Angular material mat-error cannot show message

后端 未结 2 1516
刺人心
刺人心 2020-12-06 02:11

I have 2 datetime picker, endDate cannot be less than startDate

endDateAfterOrEqualValidator(formGroup): any {
    var startDateTimestamp: number;
    var en         


        
2条回答
  •  伪装坚强ぢ
    2020-12-06 03:03

    If you want to set a control as invalid from the .ts file manually...

    HTML:

    
      
      (Optional)
      
          Must be a valid input!
      
    
    

    TS:

    import { FormControl } from '@angular/forms';
    
    @Component({
      selector: 'derp',
      templateUrl: './derp.html',
      styleUrls: ['./derp.scss'],
    })
    export class ExampleClass {
    
      // Date Error Form Control
      exampleFormControl = new FormControl('');
    
      // Variable Check
      inputValid: boolean;
    
      changeDetected() {
        // Check if input valid
        if (this.inputValid) {
          console.log('Valid Input');
        } else {
          console.log('Invalid Input');
          // IMPORTANT BIT - This makes the input invalid and resets after a form change is made
          this.exampleFormControl.setErrors({
            invalid: true,
          });
        }
      }
    
      // CODE THAT CHANGES VALUE OF 'inputValid' //
    
    }
    

提交回复
热议问题