自定义 ControlValueAccessor 似乎不适用于我自己的 FormControl 指令
Custom ControlValueAccessor doesn't seem to work for my own FormControl directive
我正在尝试实现此 ,因为我一直在尝试创建自己的自定义 FormControl
,如该问题中所述。
我创建了一个自定义 ControlValueAccessor
并且它作为 valueAccessor
注入到我的 FormControl
指令的构造函数中,但是当我尝试使用 [=14 注册回调时=] 函数永远不会被调用。
事实上 none 的成员似乎被召唤了。
我已经阅读了很多文章和博客文章,但无法弄清楚我遗漏了什么或做错了什么。
我从原来的 to demonstrate here 扩展了 plunkr。
如有任何帮助或指点,我们将不胜感激。
目标就像另一个问题一样@Component({..., template: '<input myFormControl...'}...
注册FormControls。
你快到了:)。不过还有一个窍门。
您的自定义 NgControl
没有调用 FormControlDirective
的 ngOnChanges
中 is called 的 setUpControl
函数。那是因为你的自定义控件的 @Inputs
没有改变。此外,您不在控件上使用任何输入。
但您可以 运行 手动:
import { SimpleChange } from '@angular/core';
export class HybridFormControlDirective extends FormControlDirective {
/* @override */
constructor(...) {
super(validators, asyncValidators, valueAccessors);
this.form = new FormControl('');
this.form.patchValue('test2');
// manually call ngOnChanges to make sure that setUpControl is called
this.ngOnChanges({
form: new SimpleChange(undefined, this.form, true)
});
}
}
我正在尝试实现此 FormControl
,如该问题中所述。
我创建了一个自定义 ControlValueAccessor
并且它作为 valueAccessor
注入到我的 FormControl
指令的构造函数中,但是当我尝试使用 [=14 注册回调时=] 函数永远不会被调用。
事实上 none 的成员似乎被召唤了。
我已经阅读了很多文章和博客文章,但无法弄清楚我遗漏了什么或做错了什么。
我从原来的
如有任何帮助或指点,我们将不胜感激。
目标就像另一个问题一样@Component({..., template: '<input myFormControl...'}...
注册FormControls。
你快到了:)。不过还有一个窍门。
您的自定义 NgControl
没有调用 FormControlDirective
的 ngOnChanges
中 is called 的 setUpControl
函数。那是因为你的自定义控件的 @Inputs
没有改变。此外,您不在控件上使用任何输入。
但您可以 运行 手动:
import { SimpleChange } from '@angular/core';
export class HybridFormControlDirective extends FormControlDirective {
/* @override */
constructor(...) {
super(validators, asyncValidators, valueAccessors);
this.form = new FormControl('');
this.form.patchValue('test2');
// manually call ngOnChanges to make sure that setUpControl is called
this.ngOnChanges({
form: new SimpleChange(undefined, this.form, true)
});
}
}