Javascript 角度验证can';不行
每当我尝试将else块与json进行比较时,我都无法验证对json数据的输入。请帮我解决这个问题Javascript 角度验证can';不行,javascript,angularjs,json,Javascript,Angularjs,Json,每当我尝试将else块与json进行比较时,我都无法验证对json数据的输入。请帮我解决这个问题 <body ng-app="fileGetting" ng-controller="loadFile"> <label>Firstname:</label><input type="text" ng-model="placeFile.fname"><br> <label>Lastname:</label&g
<body ng-app="fileGetting" ng-controller="loadFile">
<label>Firstname:</label><input type="text" ng-model="placeFile.fname"><br>
<label>Lastname:</label><input type="text" ng-model="placeFile.lname"><br>
<button ng-click="fun()">Submit</button><br>
<div ng-repeat="x in placeFile">
<p>{{x.fname}}</p>
</div>
<script>
angular.module("fileGetting", [])
.controller("loadFile", function($scope, $http) {
$http.get("exam.json").then(function(response) {
$scope.placeFile = response.data.names;
var x = $scope.placeFile;
$scope.fun = function() {
angular.forEach(x, function(value, key) {
if ($scope.placeFile.fname == x.key && $scope.placeFile.lname == x.key)
{
alert("hi ram");
}
else
{
alert("this is incorrect");
}
});
}
});
});
</script>
如果我正确理解您正在做的事情,则以下操作应该有效: 第一次改变
<label>Firstname:</label><input type="text" ng-model="placeFile.fname"><br>
<label>Lastname:</label><input type="text" ng-model="placeFile.lname"><br>
问候
马格纳斯最终我得到了一个答案,我要感谢马格纳斯和西蒙
<body ng-app="myApp" ng-controller="loadFile">
<label>Firstname:</label><input type="text" ng-model="current.fname"><br>
<label>Lastname:</label><input type="text" ng-model="current.lname"><br>
<button ng-click="fun()">Submit</button><br>
<script>
angular.module("myApp", [])
.controller("loadFile", function($scope, $http) {
$scope.current = {fname:"", lname:""};
$http.get("exam.json").then(function(response) {
$scope.placeFile = response.data.names;
$scope.fun=function(){
$scope.placeFile.forEach(function(itm) {
if ($scope.current.fname===itm.fname && $scope.current.lname === itm.lname ) {
alert("Hi Ram");
}
});
}
});
});
</script>
名字:
姓氏:
提交
angular.module(“myApp”,[])
.controller(“加载文件”,函数($scope,$http){
$scope.current={fname:,lname:};
$http.get(“exam.json”).then(函数(响应){
$scope.placeFile=response.data.names;
$scope.fun=函数(){
$scope.placeFile.forEach(函数(itm){
if($scope.current.fname==itm.fname&&$scope.current.lname==itm.lname){
警报(“Hi Ram”);
}
});
}
});
});
你能发布你的json数据吗?嗨,fname和lname永远不会等于密钥$scope.placeFile.fname==x.key&&$scope.placeFile.lname==x.key这里的key将等于'fname'和'lname',而$scope.placeFile.fname将等于“Ram”或“Chandran”或“Jaynath”,您到底想比较什么?感谢您的回复,实际上我试图将我的Html输入值与json值进行比较。例如,我的名字:Ram和Lastname:Chandru,如果我单击submit按钮,它必须向hi Ram msgThank发出警报,感谢您的回复magnus,您在pgm中非常了解,但我必须添加submit按钮,当我单击submit按钮时,它必须调用fun功能。并且在您的程序中,当前未定义错误已发生。我尝试了很多方法,但都没能解决这个错误。如果你能帮助我,那将对我有帮助
<label>Firstname:</label><input type="text" ng-model="current.fname"><br>
<label>Lastname:</label><input type="text" ng-model="current.lname"><br>
angular.module("myApp", [])
.controller("loadFile", function ($scope, $http) {
$scope.current = { "fname": "", "lname": "" };
$scope.placefile = [];
$http.get("exam.json").then(function (response) {
$scope.placeFile = response.data.names;
});
$scope.fun = function () {
$scope.placeFile.forEach(function (itm) {
if (itm.fname === $scope.current.fname
&& itm.lname === $scope.current.lname) {
alert("Hi Ram");
}
else {
alert("incorrect");
}
});
};
});
<body ng-app="myApp" ng-controller="loadFile">
<label>Firstname:</label><input type="text" ng-model="current.fname"><br>
<label>Lastname:</label><input type="text" ng-model="current.lname"><br>
<button ng-click="fun()">Submit</button><br>
<script>
angular.module("myApp", [])
.controller("loadFile", function($scope, $http) {
$scope.current = {fname:"", lname:""};
$http.get("exam.json").then(function(response) {
$scope.placeFile = response.data.names;
$scope.fun=function(){
$scope.placeFile.forEach(function(itm) {
if ($scope.current.fname===itm.fname && $scope.current.lname === itm.lname ) {
alert("Hi Ram");
}
});
}
});
});
</script>