javascript - “if”和“else if”条件都在过滤器函数中执行(Angular-8,JavaScript)
问题描述
“if”和“else if”条件都在过滤器函数中执行。当我尝试执行下面的程序时,我得到的输出为
this.users.filter((hero)=> {
if(hero.name === this.profileForm.value.name && hero.password === this.profileForm.value.password){
this.router.navigate(['/dashboard']);
console.log("in if")
}else if(hero.name !== this.profileForm.value.name){
console.log("in else if")
this.isValid=false;
}
任何人都可以帮助我代码有什么问题。
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl,Validators } from '@angular/forms';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private router:Router) { }
ngOnInit() {
}
isValid:boolean=true;
users =[{
name:"user1",
password:"password1"
},
{
name:"user2",
password:"password2"
}
]
profileForm = new FormGroup({
name: new FormControl('',Validators.required),
password: new FormControl('',[Validators.minLength(6),Validators.required]),
});
onSubmit(){
this.users.filter((hero)=> {
if(hero.name === this.profileForm.value.name && hero.password === this.profileForm.value.password){
this.router.navigate(['/dashboard']);
console.log("in if")
}else if(hero.name !== this.profileForm.value.name){
console.log("in else if")
this.isValid=false;
}
});
}
}
<div class="container">
<div class="login-page">
<h2 >Welcome to Timesheet Tracker</h2>
<div class="form">
<form class="login-form" [formGroup]="profileForm" (ngSubmit)="onSubmit()">
<input type="text" placeholder="username" formControlName="name"/>
<span *ngIf="!profileForm.get('name').valid && profileForm.get('name').touched" class="help-block">Please enter user name..!</span>
<input type="password" placeholder="password" formControlName="password"/>
<span *ngIf="!profileForm.get('password').valid && profileForm.get('password').touched" class="help-block">Please enter valid password..!</span>
<button type="submit" >login</button>
</form>
<p class="par" *ngIf="!isValid">Please enter valid user name and password..!</p>
</div>
</div>
</div>
解决方案
你不需要.filter
这里。.filter
迭代每个值。对于某些值,满足一个条件,对于另一些值,则满足另一个条件,因此两者if
似乎else
都被执行。
你需要做的是这样的:
let heroFound = false;
for(let hero of this.users){
if(hero.name === this.profileForm.value.name && hero.password ===
this.profileForm.value.password){
heroFound = true;
break;
}
}
if(heroFound){
this.router.navigate(['/dashboard']);
}else{
this.isValid=false;
}
推荐阅读
- ios - 无法使用“capacitor js”命令在 Xcode 上构建
- python - 用 DF 列文本 Matplotlib 注释散点图点
- python - 如何让我的 Python 代码执行得更快?
- php - 如何在 woocommerce My-account -> 地址中向前端显示自定义地址字段值?
- charts - 需要在tradingview中删除前一天的情节 - pinescript
- reactjs - 如何降低 Material Toolbar 高度 Material-UI?
- python - 如何在 html 输入中显示占位符?
- cassandra - 如何正确地将客户端应用程序连接到 Scylla 或 Cassandra?
- c# - 在 Visual Studio 2019 c-sharp 中检索 SQL Server 2019 表列列表的 ExecuteScalar 方法错误地按 ASCII 排序
- javascript - 如何使用 javascript 清除谷歌翻译源文本 div?