是否可以使用 ngModel 对自定义指令执行双向绑定?

Is it possible to perform a two way binding on a custom directive with ngModel?

我的用例完全不同。当我剥离所有其他因素时,归结为这一点。

假设我有以下输入元素

<input type="text" [customDirective] [(ngModel)]="myValue" >

customDirective 的工作是查看用户输入的值并根据输入动态更改其值。

这个怎么实现双向绑定

我玩过 ControlValueAccessor、DefaultValueAccessor。但是无论我做什么,我都无法实现双向绑定。我一次达到的最大值是模型更新时的视图更新,而不是相反。但是该代码丢失了。

这是香草味的plunker link.

PS: 我已经提到了以下内容。但其中 none 有助于实现 w.r.t 到指令

的双向绑定

http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html

提前致谢

终于知道怎么做了。

模型到 UI 的更改可以使用 ControlValueAcessor

完成

UI 模型可以像下面这样完成

import {Output} from '@angular/core';

使用事件发射器

@Output() ngModelChange = new EventEmitter();

每当数据改变时发出事件

this.ngModelChange.emit(YOUR_NEW_VALUE);

这里是详细的例子

Using Tinymce editor as a directive