I have a login portal in angular6 while login i have a list of users arr and any user from that list can login below is the code
loginComponent.ts
import {Component} from '@angular/core';
import {Router} from "@angular/router";
@Component({
selector : 'app-root',
templateUrl : 'login.html'
})
export class loginCompClass{
userArr = [{"username":"admin","password":123456},{"username":"demo","password":123456}];
constructor(private router: Router) { }
msg = "";
login = function() {
this.userArr.forEach(function(element) {
if(this.username == element.username && this.password == element.password){
this.msg = "Logged in successfully";
this.router.navigate(['/dashboard']);
}
});
}
}
But problem is that condition in IF ForEach loop not working properly it gives me this is undefined
login_template.html
<form>
<input type="text" [(ngModel)] = "username" placeholder="Username" name="username"/><br>
<input type="password" [(ngModel)] = "password" placeholder="Password" name="password"/><br><br>
<input type="button" value="LOGIN" (click)="login()"/>
</form>
<h1>{{msg}}</h1>
Please suggest a way to solve this