繁体   English   中英

如何在降级的Angular组件上实现ControlValueAccessor

[英]How to implement ControlValueAccessor on downgraded Angular component

我有一个实现ControlValueAccessor的Angular组件,但是从未使用ngModel的初始值调用writeValue方法。

模板:

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

该组件通过以下方式降级为AngularJS:

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

我尝试将ngModel添加到inputsoutputs但无法正常工作。

您应该使用ng-model属性而不是[(ngModel)] ,如下所示: <my-comp ng-model="$ctrl.activeUser"></my-comp>

从角度文档:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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