2016-12-26 55 views
0

我想使用AngularJS進行非常簡單的註冊。 首先,我得到這個電子郵件的用戶並分配給$ scope.users。如果方法「GetUserByEmail」返回多個用戶,我嘗試顯示消息「用戶已經存在」。這是問題。方法GetUserByEmail被避免。程序「跳轉」到「如果」條件和$ scope.users總是空的,我不知道爲什麼。將用戶添加到數據庫後有時,該方法返回對象的數組,並分配給$ scope.users使用AngularJS註冊

這是我的代碼與方法CREATEUSER:

var RegisterController = function ($scope, Api, $http) { 
 

 
    $scope.users = { 
 
    } 
 

 
    $scope.CreateUser = function() { 
 
     var user = { 
 
      Password: $scope.password, 
 
      Name: $scope.name, 
 
      Surname: $scope.surname, 
 
      Email: $scope.email, 
 
      DateOfBirth: $scope.dateofBirth 
 
     } 
 

 
     Api.GetUserByEmail("Users", "GetUserByEmail", $scope.email).then(function (d) { 
 
       $scope.users = d; 
 
      }); 
 

 
     if ($scope.users.length > 0) 
 
     { 
 
      alert("User already exists!"); 
 
      $scope.users = {}; 
 
     } 
 
     else 
 
     { 
 
      Api.PostUser("Users", "PostUser", user).then(function (d) { 
 
       alert("Hello"); 
 
      }); 
 
     } 
 

 

 
    }; 
 
} 
 

 
RegisterController.$inject = ['$scope', 'Api', '$http'];

和方法GetUserByEmail:

this.GetUserByEmail = function (controllerName, methodName, email) { 
 
     var promise = $http({ 
 
      method: 'GET', 
 
      url: 'api/' + controllerName + '/' + methodName + '?email=' + email, 
 
      config: { 
 
       params: { 
 
        "email": email 
 
       } 
 
      } 
 
     }) 
 
     .then(function onSuccess(response) { 
 
      return response.data; 
 
     }, 
 
     function onError(response) { 
 
      return response.statusText; 
 
     }); 
 
     return promise; 
 
    }

回答

0

如果您在請求中使用的成功......錯誤塊沉綿響應爲空,請求將返回到成功的街區,你可以驗證侑數據。我認爲這些都很好。

var RegisterController = function ($scope, Api, $http) { 

    $scope.users = { 
    } 

    $scope.CreateUser = function() { 
     var user = { 
      Password: $scope.password, 
      Name: $scope.name, 
      Surname: $scope.surname, 
      Email: $scope.email, 
      DateOfBirth: $scope.dateofBirth 
     } 

     Api.GetUserByEmail("Users", "GetUserByEmail", $scope.email).success(function (response) { 
       if (response!=null && dresponse.lenght > 0) 
     { 
      alert("User already exists!"); 
      $scope.users = {}; 
     } 
     else 
     { 
       Api.PostUser("Users", "PostUser", user) 
       .success(function (d) { 
        alert("Hello"); 
      }).error(function(e){ 
      console.log(e.message); 
      }); 
     } 
      }).error(function(error){ 
       console.log(error.message); 
      }); 
    }; 
} 

RegisterController.$inject = ['$scope', 'Api', '$http']; 
+1

它也行得通,謝謝! :) – ryckoshet

1

試試這個!

var RegisterController = function($scope, Api, $http) { 
 

 
    $scope.users = {} 
 

 
    $scope.CreateUser = function() { 
 
    var user = { 
 
     Password: $scope.password, 
 
     Name: $scope.name, 
 
     Surname: $scope.surname, 
 
     Email: $scope.email, 
 
     DateOfBirth: $scope.dateofBirth 
 
    } 
 

 
    Api.GetUserByEmail("Users", "GetUserByEmail", $scope.email).then(function(d) { 
 
     $scope.users = d; 
 
     if ($scope.users.length > 0) { 
 
     alert("User already exists!"); 
 
     $scope.users = {}; 
 
     } else { 
 
     Api.PostUser("Users", "PostUser", user).then(function(d) { 
 
      alert("Hello"); 
 
     }); 
 
     } 
 
    }); 
 

 
    }; 
 
} 
 

 
RegisterController.$inject = ['$scope', 'Api', '$http'];

+1

謝謝,它工作:)。 – ryckoshet