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

标签 angular angular-components

我有一个 Angular 组件实现了 ControlValueAccessor ,但是 writeValue永远不会使用 ngModel 中的初始值调用方法.

模板:

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

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

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

我尝试添加 ngModelinputsoutputs但它不起作用。

最佳答案

你应该使用 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/

相关文章:

javascript - ionic 确认警报 : await click on alert before continuing

Angular 2 : Passing data to child components

angular - 控制台出错但在浏览器中显示

typescript - 如何在应用程序首先加载而不是 Angular 5 中的 appComponent 时显示登录组件

angular - 每行和标题列的 "Edit/Delete"按钮在 md-table 组件中为 'Action'

Angular - 在服务值发生变化时更新组件值

angular - 内容类型 `application/x-www-form-urlencoded` 的编码对象

java - 如何从 Angular 2 应用程序执行驻留在服务器上的 Java 代码?

node.js - Angular 4 CanActivate 防护不会加载与服务一起使用的组件;简单的 Observable.of(true) 有效

angular-cli - 角度 5 延迟加载与动态加载