本文介绍了如何在自定义元素上实现ngModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个简单的输入元素,我可以做到这一点:
Given a simple input element I can do this:
<input [(ngModel)]="name" /> {{ name }}
这不适用于我的自定义元素:
This doesn't work for my custom elements:
<my-selfmade-combobox [(ngModel)]="name" values="getValues()" required></my-selfmade-combobox>
我该如何实施?
推荐答案
我认为此链接将回答您的问题:
I think this link will answer your question:
我们需要实现两件事来实现这一目标:
We need to implement two things to achieve that:
- 提供表单组件逻辑的组件.它不需要输入,因为它将由ngModel本身提供
- 自定义
ControlValueAccessor
,它将实现此组件与ngModel
/ngControl
之间的桥梁
- A component that provides the logic of your form component. It doesn't need an input since that will be provided by ngModel itself
- A custom
ControlValueAccessor
that will implement the bridge between this component andngModel
/ngControl
上一个链接为您提供了完整的示例...
The previous link gives you a complete sample...
这篇关于如何在自定义元素上实现ngModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!