1

To get access to NgControl in my component I create constructor:

NgControl _cd;
MyDateEditComponent(@Self() @Optional() this._cd);

I want use my component like this:

<my-date-edit [(ngModel)]="date" ngControl="date">

or

<my-date-edit [(date)]="date" ngControl="date">

Now I have error:

Assertion failed: "No value accessor for (date) or you may be missing formDirectives in your directives list."

How to fix it?

Cododoc
  • 125
  • 8

1 Answers1

3

if you are using a ngModel on a custom component use the ControlValueAccessor

export class YourComponent implements ControlValueAccessor { 
    // need to imlement following methods
    onChange = (val: Date) => { };
    onTouched = () => { };

    writeValue(val: Date) {
        // your ngModel value comes under here
    }

    registerOnChange(fn: (val: Date) => void) {
        this.onChange = fn;
    }

    registerOnTouched(fn: () => void): void {
        this.onTouched = fn;
    }
}
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80