Angularjs 2 rc 5形成自定义验证

时间:2016-08-25 15:37:21

标签: angular

当我在表单标记中包含'[(ngModel)] =“form”'时,我尝试向表单添加自定义验证。我开始收到错误。

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]
       });

  } 

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。由于原因,我不明白ngModel在这里不能很好地发挥。

尝试使用formControlName =“name”而不是