简体   繁体   English

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

[英]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添加到inputsoutputs但无法正常工作。

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.

相关问题 Angular 2-ControlValueAccessor-如何实现? - Angular 2 - ControlValueAccessor - How to implement? 如何对自定义组件进行角度单元测试(实现 ControlValueAccessor 和继承的父表单控件) - How to do angular unit testing for a customize component (implement ControlValueAccessor and inherited parent form control) 带有 ControlValueAccessor 测试的 Angular 2(4) 组件 - Angular 2(4) component with ControlValueAccessor testing 如何将NgControl状态传递给Angular中的子组件,实现ControlValueAccessor? - How to pass NgControl status to child component in Angular, implementing ControlValueAccessor? 降级的Angular2组件未在angular 1内渲染 - Downgraded Angular2 component not rendered inside angular 1 将AngularJS数组传递给降级的Angular 7组件 - Passing an AngularJS Array to a Downgraded Angular 7 Component 在 Angular 降级组件中访问 $stateParams - Accessing $stateParams within an Angular downgraded component Animation 降级后的内容早早消失 Angular 组件 - Animation content disappearing early in downgraded Angular component Angular:用于实现嵌套表单的复合ControlValueAccessor - Angular: composite ControlValueAccessor to implement nested form 在AngularJs 1.7.2中使用NgRef降级Angular 5组件 - Using NgRef on downgraded Angular 5 component in AngularJs 1.7.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM