简体   繁体   中英

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.

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">`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM