我在我的一個表單中使用了ng-boostrap datepicker字段,我想用默認值初始化它。設置ng-bootstrap字段的默認值(ngModels不能使用getter/setter)
在「標準」的形式,我可以使用formControlName
或ngModel
,它工作正常:
<!-- formControlName -->
<input ngbDatepicker #d="ngbDatepicker" formControlName="dateFieldName">
<!-- ngModel -->
<input ngbDatepicker #d="ngbDatepicker" [(ngModel)]="myDateProp">
我的問題是我一直包在我自己的自定義字段中的日期選擇器,即自定義組件實現ControlValueAccessor
。
在這種情況下,datepicker不再知道其父窗體和相應的模型。那麼我怎樣才能初始化datepicker字段?
formControlName
不是一個選項(我的自定義字段組件不能也不應該訪問其父表單的模型)。ngModel
似乎比較容易,因爲它只是一個綁定到本地屬性,但只要我將[(ngModel)]
指令添加到datepicker字段,我的整個表單就會凍結。
下面是我的自定義字段組件的代碼至今:
@Component({
selector: 'field-datetime',
template: `
<div class="input-group">
<input class="form-control" ngbDatepicker #d="ngbDatepicker">
<div class="input-group-addon" (click)="d.toggle()" >
<i class="fa fa-calendar" style="cursor: pointer;"></i>
</div>
</div>`,
providers: [
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => FieldDateTimeComponent), multi: true }
]
})
export class FieldDateTimeComponent implements ControlValueAccessor {
private _dateTimeObj: DateTime; // Date is stored as an object internally
propagateChange = (_: any) => {};
// Write a new value to the element (from the form model into the view).
writeValue(timestamp: number) {
// Parse the timestamp to store it as an object internally.
this._dateTimeObj = moment(timestamp).toObject();
this.propagateChange(this._dateTimeObj);
}
registerOnChange(fn: any) {
this.propagateChange = fn;
}
registerOnTouched(fn: any) {}
// This is the property I'd like to bind to the field, i.e.:
// <input ngbDatepicker [(ngModel)]="dateObj">
// THIS IS WRONG. DO NOT USE A GETTER FOR A NGMODEL. SEE ANSWER BELOW.
get dateObj() {
return {day: this._dateTimeObj.date, month: this._dateTimeObj.months, year: this._dateTimeObj.years};
}
}
[(ngModel)]是正確的解決方案。你可以添加一些代碼嗎?作爲一個瘋狂的猜測,我可以想象你的ControlValueAccessor實現中的某個地方存在一個無限循環,比如writeValue調用一個調用writeValue的setter,或者類似的東西。 –
@VincentV。謝謝你的時間。我用一些代碼更新了這個問題。 – AngularChef