2016-08-25 53 views
0

我試圖添加一個自定義驗證到我的表單中,當我在表單標籤中包含'[(ngModel)] =「form」'我開始出現錯誤。Angularjs 2 rc 5表單自定義驗證

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in  pages/login.component.html:8:7 
ORIGINAL EXCEPTION: 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. 

    Example 1: <input [(ngModel)]="person.firstName" name="first"> 
    Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}"> 

但我在輸入中包含了name屬性。如果我可以將自定義驗證添加到類中的單個元素,則可以將輸入作爲獨立模型。

<div class="loginFormContainer fixedContainer"> 
     <form [(ngModel)]="form" #loginForm="ngForm"> 
      <div class="loginTitle"> 
      Sign in to xxxx 
      </div> 
      <div> 
       <span class="fieldLabel">Login name</span> 
      </div> 
      <input type="text" class="form-control" [(ngModel)]="model.name" name="name" #name="ngModel" class="inputField medium" > 
     <div> 

打字稿代碼

export class LoginComponent 
{ 
    form: ControlGroup; 
    model = new Login("", ""); 

    constructor(
     private route: ActivatedRoute, 
     private router: Router, 
     private loginServ : LoginService, 
     fb:FormBuilder 
    ){ 
     this.form = fb.group({ 
     user:['', Validators.required], 
     pass:['', Validators.required] 
     }); 

    } 

回答

0

我有同樣的問題。由於我不明白的原因,ngModel在這裏播放不好。

嘗試使用formControlName =「name」代替