如何在自定义元素上实现 ngModel? [英] How to implement ngModel on custom elements?

查看:22
本文介绍了如何在自定义元素上实现 ngModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个简单的 input 元素,我可以这样做:

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 and ngModel / ngControl

上一个链接为您提供了一个完整的示例...

The previous link gives you a complete sample...

这篇关于如何在自定义元素上实现 ngModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆