- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
创建使用 ngModel
的自定义表单元素时,必须提供 NG_VALUE_ACCESSOR
,这又要求组件实现 ControlValueAccessor
,它有一个名为 registerOnTouched
的方法。所有 angular.io 文档都是这样说的:
registerOnTouched(fn: any) : void
Set the function to be called when the control receives a touch event.
Angular 文档听起来像是触摸输入,但我认为这是 ngForm
查看用户是否曾与输入交互的方式的一部分。文档中似乎缺少一些关于什么是“触摸事件”以及应该如何从组件外部注册和使用“onTouch”处理程序的信息。
这到底是为了什么?它是如何使用的?谁提供了fn
? ...开发人员使用我的组件 ((touched)="handler()"
),还是仅使用 Angular 本身?我见过一些简单地注册处理程序的示例,然后从不调用它,或者忽略它。如果是这样,我为什么还需要实现它?
“触摸事件”的端到端示例可能会清除这一切。
最佳答案
An issue was created和 the documentation for registerOnTouched此后发生了变化:
Registers a callback function that should be called when the control receives a blur event.
This is called by the forms API on initialization so it can update the form model on blur.
...
参见 the documentation for registerOnTouched .
注册触摸处理程序会导致
ngForm
正确设置
ng-touched
类和
ng-untouched
自定义输入上的类。
Here's an example on plunker.
我不确定触摸事件由什么组成,也不知道在触摸自定义输入时直接检查或收听的方法。
关于angular - registerOnTouched 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317462/
创建使用 ngModel 的自定义表单元素时,必须提供 NG_VALUE_ACCESSOR ,这又要求组件实现 ControlValueAccessor ,它有一个名为 registerOnTouch
我是一名优秀的程序员,十分优秀!