简体   繁体   English

使用 ngSubmit 在 Angular2 中登录表单

[英]Login Form in Angular2 using ngSubmit

I have a login form where I want to validate the email id and password.我有一个登录表单,我想在其中验证 email id 和密码。 I have tried passing the id and password to the typescript file.我尝试将 ID 和密码传递给 typescript 文件。 But only the email id is being passed not the password.但只有 email id 被传递,而不是密码。 Here's the HTML code.这是 HTML 代码。

 <form #instituteForm="ngForm" (ngSubmit)="instituteLogin(instituteForm)"> <div id="user-data"> <div class="form-group"> <label for="email">Facility ID</label> <input type="email" id="email" name="facilityId" class="form-control" [(ngModel)]="institute.facilityId"> <span class="help-block" *ngIf=".email.valid && email.touched">Please enter a valid email!</span> <label for="password">Passkey</label> <input type="password" id="password" name="facilityPasskey" class="form-control" [(ngModel)]="institute.facilityPasskey"> </div> </div> <button class="btn btn-primary" type="submit">Login</button> </form>

 institute = { facilityId: '', facilityPasskey: '' }; submitted = false; instituteLogin(instForm: NgForm){ console.log("Going from facility to login"); //DONE: instForm.value['facilityID'] // instForm.value['facilityPasskry'] console.log(instForm.value); {this.router.navigateByUrl('/login/olduser');} }

Output on console控制台上的 Output

Checkout this StackBlitz.查看此 StackBlitz。 StackBlitz Link of problem StackBlitz 问题链接

There is validation error, You forgot to add #facilityId="ngModel" in input and change validation by存在验证错误,您忘记在输入中添加#facilityId="ngModel"并通过更改验证

 <span class="help-block" *ngIf="!facilityId.valid &&
 facilityId.touched">Please enter a valid email!</span>

so after changes you can get both value, check stackblitz因此更改后您可以获得两个值,请检查stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM