我有一个 Angular 组件实现了 ControlValueAccessor
,但是 writeValue
永远不会使用 ngModel
中的初始值调用方法.
模板:
<my-comp [(ngModel)]="$ctrl.activeUser"></my-comp>
组件通过以下方式降级为 AngularJS:
.directive('myComp', downgradeComponent({
component: MyComp,
inputs: [],
outputs: [],
}));
我尝试添加 ngModel
至 inputs
和 outputs
但它不起作用。
最佳答案
你应该使用 ng-model
属性而不是 [(ngModel)]
,像这样:<my-comp ng-model="$ctrl.activeUser"></my-comp>
来自 Angular 文档:
* 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">`
关于angular - 如何在降级的 Angular 组件上实现 ControlValueAccessor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48814042/