0

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

Mamta
  • 6,426
  • 1
  • 13
  • 31
  • This might help you along: https://stackoverflow.com/a/3127440/2232127 – JensV Jun 17 '19 at 11:42
  • Use arrow function i.e. `this.userArr.forEach((element) => {` instead of conventional anonymous method with forEach – Satpal Jun 17 '19 at 11:43

0 Answers0