我正在尝试创建一个自定义组件以在有角度的 formGroup 中使用。
这是我想使用这个自定义组件的方式:
<form [formGroup]="form">
...
<app-date-picker formControlName="dateStart"
[isConsultation]="isConsultation"
[label]="'Du'"
[(ngModel)]="agenda.datDeb">
</app-date-picker>
...
</form>
问题:在主组件(包含此表单)中,当我的自定义组件中的值发生更改时,模型没有更新,这涉及 MdDatePicker。虽然我使用的是 ControlValueAccessor。
我的客户组件的 HTML 模板:
<div class="">
<span *ngIf="label">{{label}} :</span>
<md-form-field class="" [ngClass]="isConsultation ? 'no-icon' : 'container-input-date'">
<input mdInput
[mdDatepicker]="pickerDebut"
class="consultation"
[(ngModel)]="theDate">
<md-datepicker-toggle mdSuffix [for]="pickerDebut"></md-datepicker-toggle>
<md-datepicker #pickerDebut></md-datepicker>
</md-form-field>
</div>
在这里,您可以阅读我的组件的打字稿代码:
import {Component, Input, ViewChild, forwardRef} from '@angular/core';
import {
NgModel,
ControlValueAccessor,
NG_VALUE_ACCESSOR,
NG_VALIDATORS,
FormControl,
} from "@angular/forms";
export function validateDateInputFormat(c: FormControl) {
// Error content in case input date format is not valid
let err = {
formatError: {
given: c.value,
acceptedFormat: 'dd/MM/yyyy'
}
};
console.log('VALIDATE => c : ', c);
// Control logiq
// return c.value == null ? null : (String(c.value).match(date_regexp)) ? null : err;
return null;
}
@Component({
selector: 'app-date-picker',
templateUrl: './date-picker.component.html',
styleUrls: ['./date-picker.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DatePickerComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useValue: validateDateInputFormat,
multi: true
}
]
})
export class DatePickerComponent implements ControlValueAccessor {
@Input()
label : string;
@Input()
isConsultation : boolean;
@ViewChild(NgModel) _theDate: NgModel;
constructor() { }
propagateChange = (_: any) => {};
onTouched: any = () => { };
writeValue(obj: any): void {
console.log('writeValue => obj : ', obj);
if (obj) {
this._theDate = obj;
}
}
registerOnChange(fn: any): void {
this.onChange = fn;
console.log('registerOnChange => fn : ', fn);
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
console.log('registerOnTouched => fn : ', fn);
}
onChange(event){
console.log('onChange(event) - event => ', event );
this.propagateChange(event.target.value);
}
get theDate() {
console.log('get theDate()');
return this._theDate;
}
set theDate(val) {
console.log('set theDate(val) - val => ', val );
this._theDate = val;
this.propagateChange(val);
}
}
我在这里做错了什么?