我正在尝试使用primeNG日历模块和reactiveFormsModule,但是当我一起使用它们时,日历会消失。
import {Common Module} from '@angular/common';
import {CalendarModule} from 'primeng/primeng';
import {ReactiveFormsModule} from '@angular/forms';
@NgModule({
imports: [CommonModule, ReactiveFormsModule, CalendarModule]
})
和
<form [formGroup]="myFormName">
<p-calendar formControlName="myFormControl"></p-calendar>
</form>
任何建议。
答案 0 :(得分:2)
以下是使用p-calendar
的反应形式的简单示例。
HTML:
<form [formGroup]="form" class="box-model form-support-margin">
<div>
<p-calendar [dateFormat]="dateFormat"
[showIcon]="true" formControlName="tbDate" [readonlyInput]="true">
</p-calendar>
</div>
</form>
<p>Date from form:</p>
<p>{{form.value | json}}</p>
TS:
import { Component } from "@angular/core";
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'input-overview-example',
templateUrl: 'input-overview-example.html',
styleUrls:['input-overview-example.css']
})
export class InputOverviewExample {
dateFormat: = "mm/dd/yy"
form : any;
tbDate : any;
constructor( private fb: FormBuilder){}
ngOnInit() {
this.initFormGroup();
}
initFormGroup() {
this.form = this.fb.group({
tbDate: new FormControl(this.tbDate || '', Validators.required);
});
}
}
答案 1 :(得分:0)
已接受的答案对我不再有用。 我看到了许多表单示例,这些示例显示了如何使用表单构建器,但没有提供有关验证的信息,我认为这是任何表单必不可少的一部分。
这就是我解决它的方式。希望对您有所帮助。
html:
<form [formGroup]="form" (ngSubmit)="submitParams()">
<div class="form-group row">
<div class="col-sm-6">
<div>
<h1>Foo</h1>
<input id="foo" type="text" class="form-control" formControlName="foo" placeholder="Enter Foo" required />
<div *ngIf="getFormControl('foo').invalid && (form.controls['foo'].dirty || form.controls['foo'].touched)" class="alert alert-danger"> FOO is required</div>
</div>
</div>
<div class="col-sm-6">
<div>
<h1>DUE DATE</h1>
<p-calendar formControlName="dueDate" placeholder="Select Date"></p-calendar>
<div *ngIf="getFormControl('dueDate').invalid && (form.controls['dueDate'].dirty || form.controls['dueDate'].touched)" class="alert alert-danger col-sm-9"> Due Date is required</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<button type="submit" [disabled]="form.pristine || form.invalid">Generate</button>
</div>
</div>
</form>
ts:
import { Component } from "@angular/core";
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'input-overview-example',
templateUrl: 'input-overview-example.html',
styleUrls:['input-overview-example.css']
})
export class InputOverviewExample {
foo : FormControl
dueDate : FormControl
form : FormGroup;
constructor( private fb: FormBuilder){}
ngOnInit() {
this.initFormGroup();
}
initFormGroup() {
this.foo= this.formBuilder.control('', Validators.required);
this.dueDate = this.formBuilder.control('', Validators.required);
this.form = this.formBuilder.group({
foo: this.foo,
dueDate : this.dueDate
});
}
getFormControl(name){
return this.form.get(name);
}
submitParams(){
try{
let params = {
"foo": this.form.get('foo').value,
"duedate": this.form.get('duedate').value
};
// logic to call service
}
}
}