javascript - I can't make an input be required with Angular2 -
i'm starting angular , i'm on project have validate inputs can't left clear, every input must me completed.
it's html , have .ts file.
this extract of html:
<div class="form-group"> <input type="text" class="form-control" id="factory" [(ngmodel)]="factory.company"> </div>
i need validate factory input when watching tutorials needed write 'required' inside <input>
, had <form>
, every input inside form, , html doesn't have <form>
, when put 1 design horrible , couldn't work.
here example using required fields (in login page) :
<form [formgroup]='loginform' (submit)="login(loginform.value)"> <div class="col-md-6"> <div class="login-mail"> <input type="text" placeholder="email" formcontrolname="email" required=""> <i class="fa fa-envelope"></i> </div> <div class="login-mail"> <input type="password" placeholder="password" formcontrolname="password" pattern=".{8,20}" required=""> <i class="fa fa-lock"></i> </div> </div> <div class="col-md-6 login-do"> <label class="hvr-shutter-in-horizontal login-sub"> <input type="submit" value="login" > </label> </div> <div class="clearfix"> </div> </form>
in login.component.ts
, u should make changes:
1) import modules :
import { formbuilder, formgroup, validators} '@angular/forms';
2) in oninit function :
loginform: formgroup; constructor(private fb : formbuilder) {} ngoninit(){ this.loginform = this.fb.group({ email : ["", validators.required], password : ["", validators.required] }); }
hope helps u :)
Comments
Post a Comment