If ngModel is used within a form tag, either the name attribute must be set or the form contro

  • 背景:angular中使用form表单时报错。

报错信息:If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.

  • 原因:input使用双向绑定ngModel时,ng会给这个标签创建一个FormControl对象,并以input定义的name属性为标识。没有name属性会报错。
  • 解决方案: 1、增加name属性。

                        2、不让ng创建FormControl对象。[ngModelOptions]="{standalone: true}"

猜你喜欢

转载自blog.csdn.net/ligaoming_123/article/details/84873761