Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement ControlValueAccessor on downgraded Angular component

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.

like image 840
David Fregoli Avatar asked Feb 15 '18 18:02

David Fregoli


1 Answers

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">`
like image 161
Sergey P. aka azure Avatar answered Nov 08 '22 21:11

Sergey P. aka azure