0
比较用户输入的值,我是新来的角2,想比较的JSON数据的用户输入值。我正在附上我正在尝试编码的代码。登录表单有用户名和密码,用户需要使用json中的数据对其进行验证。如果验证是真实的,我需要与变量中与该键相关的其他相邻对象。角2:在JSON
这仅仅是一个实践的代码我试图,而不是一个实际的项目
// login.component
import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { DataService } from '../data.service'
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [DataService]
})
export class LoginComponent implements OnInit {
login:any;
loginList:any;
constructor(private dataService: DataService) { }
ngOnInit() {
this.dataService.getLoginData()
.subscribe(
(data => this.login = data)
);
}
loginSubmit(value){
console.log(value);
}
}
// JSON
[{
\t "username": "jay",
\t "password": "jay",
\t "userType": "standard"
}, {
\t "username": "Admin",
\t "password": "Admin",
\t "userType": "admin"
}, {
\t "username": "newuser",
\t "password": "newuser",
\t "userType": "standard"
}, {
\t "username": "anonmyous",
\t "password": "anonmyous",
\t "userType": "standard"
}]
<div class="container" id="lgBox">
<form class="form-group" #lgForm="ngForm" (ngSubmit)="loginSubmit(lgForm.value)">
<div class="loginRow"><h4 class="text-center">Login</h4></div>
<div class="loginRow"><input type="text" #username name="username" ngModel class="form-control" placeholder="Username" required /></div>
<div class="loginRow"><input type="password" #password name="password" ngModel class="form-control" placeholder="Password" required /></div>
<div class="loginRow"><button class="btn btn-primary btn-block/">Login</button></div>
</form>
</div>
您可以使用JSON。解析到ypur JSON转换成一个对象,然后将其与用户输入 –
输入解析JSON什么是关键变量哪里是周边物体 – Aravind
要检查用户名和密码,如果它在JSON文件匹配要返回用户名,同样 – Jay