1

I'm trying to edit selected value to a custom control value accessor angular material date component but I'm it's return an empty input field.

App.Component.Html

<date-input  ngModel="dateValue"
                name="dateName">
              </date-input>

DateInputComponent.html

  <mat-form-field>
    <input [(ngModel)]="value" matInput [matDatepicker]="picker1" >
    <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
    <mat-datepicker #picker1></mat-datepicker>
  </mat-form-field>
</div>

DateInputComponent.ts

get value(): any {
    return this.innerValue;
  }

  // set accessor including call the onchange callback
  set value(v: any) {
    if (v !== this.innerValue) {
      this.innerValue = v;
      this.onChangeCallback(v);
    }
  }

  // The internal data model
  private innerValue: any = '';

  // Placeholders for the callbacks which are later provided
  // by the Control Value Accessor
  private onTouchedCallback: () => void = noop;
  private onChangeCallback: (_: any) => void = noop;

  ngOnInit() {
    // this.formControlItem = this.formControlItem[this.LabelInput.fieldName];
    this.showInput = true;
  }

  // Set touched on blur
  onBlur() {
    this.onTouchedCallback();
  }

  // From ControlValueAccessor interface
  writeValue(value: any) {
    if (value !== this.innerValue) {
      this.innerValue = value;
    }
  }

This how the value look like

Tue Feb 11 2020 00:00:00 GMT+0200 (Eastern European Standard Time)

As I said above, nothing show up in the date field if I want to edit the value

1
  • I think it's not necesary a "custom form control" to make a datePicker -you don't clearer much the code else complex the aplication. Commented Feb 6, 2020 at 9:02

1 Answer 1

1

ngModel binding syntactical error.

<date-input [(ngModel)]="dateValue" name="dateName"></date-input>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.