1. 程式人生 > >If ngModel is used within a form tag, either the name attribute must be set or the form contro

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}"