1
我正在更新P日曆模型值onblur事件,儘管formcontrol在模型更新後有效,但表單驗證將引發false。FormGroup中使用(onBlur)事件在p日曆中使用(onBlur)事件
HTML
<form (ngSubmit)="onSubmit()" [formGroup]="form" class="box-model form-support-margin">
<div class="col-md-10 col-sm-12 col-xs-12">
<p-calendar [(ngModel)]="tbDate" [dateFormat]="dateFormat"
[showIcon]="true" formControlName="tbDate" readonlyInput="false"
(onBlur)="tbDateChange($event)">
</p-calendar>
</div>
</form>
component.ts
import { Component, OnInit, EventEmitter, Pipe, ChangeDetectorRef, Input } from "@angular/core";
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import * as moment from 'moment'
import { Router } from '@angular/router';
@Component({
templateUrl: "address.component.html",
})
export class AddressComponent implements OnInit {
constructor( private fb: FormBuilder){}
ngOnInit() {
this.initFormGroup();
}
initFormGroup() {
this.form = this.fb.group({
tbDate: new FormControl(this.tbDate|| '', Validators.required);
});
}
tbDateChange(event: any) {
let time = event.srcElement.value;
let d = new Date();
this.tbDate=d;
}
}
您同時使用ngModel和formControlName和IDK的什麼'tbDateChange'甚至在做,'this.date'是什麼? – Chrillewoodz
是的,我使用ngModel和FormControlName。 tbDateChange事件用於在用戶按F11鍵時在OnBlur事件中分配當前日期。對不起this.date-> this.tbDate ngModel。 – Vignesh
您不能同時使用ngModel和formControlName。 – Chrillewoodz