[英]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
. 我有一个实现ControlValueAccessor
的Angular组件,但是从未使用ngModel
的初始值调用writeValue
方法。
template: 模板:
<my-comp [(ngModel)]="$ctrl.activeUser"></my-comp>
the component is downgraded to AngularJS via: 该组件通过以下方式降级为AngularJS:
.directive('myComp', downgradeComponent({
component: MyComp,
inputs: [],
outputs: [],
}));
I tried adding ngModel
to inputs
and outputs
but it's not working. 我尝试将ngModel
添加到inputs
和outputs
但无法正常工作。
You should use ng-model
attribute instead of [(ngModel)]
, like this: <my-comp ng-model="$ctrl.activeUser"></my-comp>
您应该使用ng-model
属性而不是[(ngModel)]
,如下所示: <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">`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.