angular验证无法完成

时间:2016-03-07 09:37:13

标签: javascript angularjs json

每当我尝试与json进行比较时,我无法验证我对json数据的输入仅执行了else块。请帮我解决这个问题。

<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>

这是Json数据:

{
    "names":[
              {
                "fname":"Ram",
                "lname":"Chandru"
              },

              {
                "fname":"Chandran",
                "lname":"Krishna"
            },

            {
                "fname":"Jayanth",
                "lname":"Jo"
            }
        ]
    }

2 个答案:

答案 0 :(得分:0)

如果我理解你正在做什么,以下应该有效:

首先改变

   <label>Firstname:</label><input type="text" ng-model="placeFile.fname"><br>
   <label>Lastname:</label><input type="text"  ng-model="placeFile.lname"><br>

<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");
                  }
             });

                };
        });

此致

马格努斯

答案 1 :(得分:0)

最终我得到了一个答案,我想感谢马格努斯和西蒙。

<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>