代码之家  ›  专栏  ›  技术社区  ›  dEs12ZER

错误类型错误:this.datefirefit.gettime不是函数:angular5

  •  0
  • dEs12ZER  · 技术社区  · 7 年前

    我正在使用angular5,我有两个输入类型date。 如果第一个日期输入大于第二个日期输入,则尝试不验证表单。

    所以有两种情况:

    1. 当dateone>datetwo时,用户将看到一条错误消息。
    2. 当DateOne 用户可以提交表单。

    以下是我试图执行的代码:

    这是.html文件:

        <div class="modal-body">
        <form [formGroup]="form" (ngSubmit)="addProjecToClients()">
            <div class="form-group row">
              <label class="col-sm-5 col-form-label">Choisir un client : </label>
    
    
              <div class="col-sm-6">
    
                <ng-select  *ngIf="_listClients"
                             [items]="_listClients"
                              bindLabel ="nom"
                              bindValue ="id"
                            [(ngModel)]="selectedPersonId"
                            formControlName="selectedPersonId"
                             >
                </ng-select>
    
              </div>
            </div>
    
            <label class="col-sm-5 col-form-label">Information liées au contrat : </label>
            <div class="form-group row">
              <label class="col-sm-5 col-form-label" >Date one :  </label>
              <div class="col-sm-6">
                <input type="date" class="form-control form-control-sm"
                       placeholder=".form-control-sm"  [(ngModel)]="dateOne"
    formControlName="dateOne">
              </div>
            </div>
    
            <div class="form-group row">
              <label class="col-sm-5 col-form-label" for="input-small">Date two : </label>
              <div class="col-sm-6">
                <input type="date" id="input-small" name="input-small" class="form-control form-control-sm"
                       placeholder=".form-control-sm"  [(ngModel)]="dateTwo" 
        formControlName="dateTwo">
    
     <span style="color:red" *ngIf="validation()">DateTwo must be greater than dateone</span>
    
              </div>
            </div>
          </div>
    
         <button type="submit" class="btn btn-primary"  [disabled]="!form.valid" >Save changes</button>
    
    </form>
    

    这是.ts文件:

     export class ProjectsComponent implements OnInit {
    
        dateOne:new Date() ;
        dateTwo:new Date(); 
    
         ngOnInit() {
    
        this.doSearch();
        this.dateOne= null;
        this.dateTwo= null;
    
     this.form = this.formBuilder.group({
          dateOne: ['', Validators.required],
          dateTwo: ['', Validators.required],
           selectedPersonId: ['', Validators.required]
        });
    
    }
    
    validation(){
        if(this.dateOne && this.dateTwo){
          if(this.dateOne.getTime() < this.dateTwo.getTime()){
            return false;
          }
          else
            return true;
        }
    
          }
    
        }
    

    但我有个错误:

    ERROR TypeError: this.dateOne.getTime is not a function
    at ProjectsComponent.validation (projects.component.ts:65)
    at Object.eval [as updateDirectives] (ProjectsComponent.html:89)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:14638)
    at checkAndUpdateView 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Krishna Mohan    7 年前

    this.dateone和this.datetwo的“typeof”是字符串,在使用gettime()函数之前需要将其转换为date()类型。

    new Date(this.dateOne).getTime()