Home > Software engineering >  How to show loggined user details on homepage
How to show loggined user details on homepage

Time:12-19

multiuser login code

$scope.users = [
  {uname: 'fida', password: 'fida', age:26, department:"science"},
  {uname: 'anu', password: 'anu', age:23,department:"maths"},
  {uname: 'nida', password: 'nida', age:20,department:"english"}
  ];
$scope.submit = function(){

  console.log($scope.users[0]);
  if($scope.users.some(x => x.uname === $scope.uname && x.password === $scope.password ||x.age )){
    $scope.name = $scope.uname;
    console.log($scope.uname);
    console.log("correct");
    $location.path('/homePage');
  }
  else{
    console.log("wrong");
    $location.path('/loginPage');
    alert("wrong username and password");
  }
  };

my aim is to show the details of that logined user on the homepage. Rightly I am able to console my user name ($scope.name) but its not showing in my homepage I don't understand why its not showing. i will also provide the HTML code of home page.

<div ng-controller="LoginpageCtrl">
<table  >
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>{{name}}</td>
        <td>kk</td>
    </tr>
</table>

CodePudding user response:

You can use the array methods some or find to check a pre populated array if the credentials are correct!

$scope.users = [
{uname: 'test', password: 'test'},
{uname: 'test1', password: 'test1'},
{uname: 'test2', password: 'test2'},
{uname: 'test3', password: 'test3'},
{uname: 'test4', password: 'test4'},
{uname: 'test5', password: 'test5'},
]

$scope.submit = function(){

  // if($scope.uname == "fida" && $scope.password == "fida"){

  if($scope.users.some(x => x.uname === $scope.uname && x.password === $scope.password)){
    console.log("correct");
    $location.path('/homePage');
  }
  else{
    console.log("wrong");
    $location.path('/loginPage');
    alert("wrong username and password");
  }
};
  • Related