如何在角度自定义组件中添加ngmodel和formcontrolname?

发布于 2025-01-29 12:06:51 字数 663 浏览 2 评论 0原文

我想用角度制作一个自定义组件。有时我将使用恢复表格来调用此组件,有时没有反应性组件。

为此,有时我会使用formControlname,有时还会使用ngmodel。 问题是我无法在自定义组件中添加这两个属性。

https://stackblitz.com/edit/ng-tie-reactive-forms--thogh-wrapper-hfpter-hfptmu?file=src%2fapp%2fapp%2fdate-wrapper%2fdate-wrapper%2fdate-wrapper.component.5.html.html

< a href =“ https://i.sstatic.net/wreyi.png” rel =“ nofollow noreferrer”>

I want to make a custom component in angular .Sometimes i will use recative forms to call this component and sometimes without reactive components.

For that ,sometimes i will use formControlName and sometimes ngmodel.
The problem is that i can not add those two properties together in my custom component.

https://stackblitz.com/edit/ng-tie-reactive-forms-throgh-wrapper-hfptmu?file=src%2Fapp%2Fdate-wrapper%2Fdate-wrapper.component.html

ngmodel and custom component together

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-02-05 12:06:51

So what you're looking for is the ControlValueAccessor interface:
https://angular.io/api/forms/ControlValueAccessor

If you implement this interface on your component, the methods of the interface will allow the component to work with both template driven and reactive forms (ngModel and formControl/formControlName). For clarity, you don't need ngModel or formControlName as Inputs on your component, this interface will cause this to be allowed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文