11

I have an Angular component that implements ControlValueAccessor, but the writeValue method is never called with the initial value from ngModel.

template:

<my-comp [(ngModel)]="$ctrl.activeUser"></my-comp>

the component is downgraded to AngularJS via:

.directive('myComp', downgradeComponent({
  component: MyComp,
  inputs: [],
  outputs: [],
}));

I tried adding ngModel to inputs and outputs but it's not working.

peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
David Fregoli
  • 3,377
  • 1
  • 19
  • 40

1 Answers1

10

You should use ng-model attribute instead of [(ngModel)], like this: <my-comp ng-model="$ctrl.activeUser"></my-comp>

From the angular docs:

 * 3. `ng-model` is controlled by AngularJS and communicates with the downgraded Angular component
 *    by way of the `ControlValueAccessor` interface from @angular/forms. Only components that
 *    implement this interface are eligible.
 *
 * ## Supported Features
 *
 * - Bindings:
 *   - Attribute: `<comp name="World">`
 *   - Interpolation:  `<comp greeting="Hello {{name}}!">`
 *   - Expression:  `<comp [name]="username">`
 *   - Event:  `<comp (close)="doSomething()">`
 *   - ng-model: `<comp ng-model="name">`
Sergey P. aka azure
  • 3,993
  • 1
  • 29
  • 23