gpt4 book ai didi

javascript - Angularjs 登录应用程序?

转载 作者:行者123 更新时间:2023-11-28 04:49:53 26 4
gpt4 key购买 nike

我想用 Angular 制作一个登录页面。我不知道如何将输入的值与硬编码值进行比较?我在这里附上我的代码,通过重写 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>

请帮助我处理 controller.js

最佳答案

您需要正确引导您的 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>

关于javascript - Angularjs 登录应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com