Angularjs登录应用程序?

时间:2016-11-23 19:19:50

标签: javascript html css angularjs

我想以角度制作登录页面。我不知道如何将输入的值与硬编码值进行比较? 我在这里附加我的代码,通过重写controller.js的代码帮助



var app = angular.module('loginApp',[])

app.controller("FormController",function($scope){
    
    $scope.details=[
        {
            name:"Shrey@gmail.com",
            password:"password"
        },
        {
            name:"amcd@gmail.com",
            password:"password"
        }
        
    
        ]
   $scope.login=function($scope){
       $scope.email='';
       
       if ($scope.details.name==$scope.email && ){
           console.log("Entered");
       }
           else{console.log("wrong");}
       }    
    
})

<!DOCTYPE html>
<html ng-app="loginApp">
<head>
<title>TO DO List</title>
<link href="css/bootstrap.css" rel="stylesheet" />
<link href="css/bootstrap-theme.css" rel="stylesheet" />
<script src="lib/angular.min.js"></script>
<script src="js/controller.js"></script>
</head>
<body>
    
<div class="container"><h2>Login App</h2></div>    
    
<form>
<div class="container" controller="FormController">    
<div class="form-group">
    <label>Email Address</label>
    <input type="email" class="form-control" ng-model="email" placeholder="Email"/>
    
    <label>Password</label>
    <input type="password" class="form-control" ng-model="password" placeholder="Password"/>
    
    
    </div>
    </div>
<div class="container">
    <button type="submit" ng-click="login()" class="btn btn-default">Submit</button>
  </div>
</form>    
    
</body>

</html>
&#13;
&#13;
&#13;

请帮我控制器.js

2 个答案:

答案 0 :(得分:0)

您需要正确引导Angular应用程序,请检查以下解决方案,以检查是否找到了与硬编码值匹配的内容。

安全问题检查此类敏感数据,例如客户端的密码属性,您应该在服务器端执行此操作。

angular
  .module('loginApp', [])
  .controller("FormController", FormController);

FormController.$inject = ['$scope'];

function FormController($scope) {
  $scope.login = login;

  var details = [{
    name: "Shrey@gmail.com",
    password: "password"
  }, {
    name: "amcd@gmail.com",
    password: "password"
  }];

  function login() {
    var isMatch = false;
    for (var i = 0; i < details.length; i++) {
      if ($scope.email === details[i].name && $scope.password === details[i].password) {
        isMatch = true;
        break;
      }
    }

    if (isMatch) {
      console.log("Entered");
    } else {
      console.log("Wrong");
    }
  }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="loginApp">
  <div ng-controller="FormController">
    <div class="container">

      <h2>Login App</h2>

      <form role="form" name="loginForm" ng-submit="loginForm.$valid && login()">

        <div class="form-group">
          <label>Email Address</label>
          <input type="email" class="form-control" placeholder="Email" ng-model="email" ng-required="true" />
        </div>

        <div class="form-group">
          <label>Password</label>
          <input type="password" class="form-control" placeholder="Password" ng-model="password" ng-required="true" />
        </div>

        <div class="form-group">
          <button type="submit" class="btn btn-default">Submit</button>
        </div>
      </form>
    </div>
  </div>
</div>

答案 1 :(得分:0)

请尝试以下方法解决您的问题

var app = angular.module('loginApp', [])

app.controller("FormCrtl", ["$scope", "$filter", function ($scope, $filter) {

    $scope.details =
         [{ name: "Shrey@gmail.com", password: "password" },
          { name: "amcd@gmail.com", password: "password" }]

    $scope.checkLoginUser = function () {
      
        if ($filter('filter')($scope.details, { name: $scope.email, password: $scope.password }).length > 0) {
            console.log("Entered");
        }
        else {
            console.log("wrong");
        }
    }


}]);
<!DOCTYPE html>
<html>
<head>
<title>TO DO List</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  
</head>
<body ng-app="loginApp">
<div class="container" ng-controller="FormCrtl">
        <h2>Login App</h2>

        <div>
            <form name="loginForm" class=" form-group">
                <label>Email Address</label>
                <input type="email" class="form-control" ng-model="email" placeholder="Email" />

                <label>Password</label>
                <input type="password" class="form-control" ng-model="password" placeholder="Password" />

                <div class="clear">
                    <input type="submit" ng-click="checkLoginUser()" class="btn btn-default" value="Submit" />
                </div>
            </form>
        </div>
    </div>

</body>

</html>