如何在角度自定义组件中添加ngmodel和formcontrolname?
我想用角度制作一个自定义组件。有时我将使用恢复表格来调用此组件,有时没有反应性组件。
为此,有时我会使用formControlname,有时还会使用ngmodel。 问题是我无法在自定义组件中添加这两个属性。
< 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您正在寻找的是ControlValueAccessor接口:
https://angular.io/api/api/api/controlvalueaeaccessor
https: / 组件,接口的方法将允许组件与模板驱动和反应性形式(ngmodel和formControl/formControlname)一起使用。为了清楚起见,您不需要ngmodel或formControlname作为组件上的输入,此接口将允许使用此界面。
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.