2017-01-03 7 views
1

私はログインアプリケーションに取り組んでおり、現在Angular JSの部分で作業しています。私がAngular JSの新人として、私はそれに固執しています。私はログインアプリケーション

public bool ValidateLogin(string email, string password) 
     { 
      try 
      { 
       ConnectionFactory conFac = new ConnectionFactory(); 
       string query = "pro_loginuser"; 
       SqlParameter[] parameter = new SqlParameter[2]; 
       parameter[0] = new SqlParameter("email", SqlDbType.NVarChar); 
       parameter[0].Value = email; 
       parameter[1] = new SqlParameter("password", SqlDbType.NVarChar); 
       parameter[1].Value = password; 
       DataTable dt = conFac.executeSelectStoredProc(query, parameter); 
       if (dt != null) 
       { 
        var isSuccess = (from c in dt.AsEnumerable() 
             select c.Field<bool>("isSuccess")).FirstOrDefault(); 
        return isSuccess; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      catch (Exception) 
      { 

       throw; 
      } 

     } 

     public CallerEntry GetUserDetails(string email,string password) 
     { 
      CallerEntry callerentry = new CallerEntry(); 
      try 
      { 
       ConnectionFactory conFac = new ConnectionFactory(); 
       string query = "pro_loginuserdetails"; 
       SqlParameter[] parameter = new SqlParameter[2]; 
       parameter[0] = new SqlParameter("email", SqlDbType.NVarChar); 
       parameter[0].Value = email; 
       parameter[1] = new SqlParameter("password", SqlDbType.NVarChar); 
       parameter[1].Value = password; 
       DataTable dt = conFac.executeSelectStoredProc(query, parameter); 
       if (dt != null) 
       { 
        callerentry = (from c in dt.AsEnumerable() 
           select new CallerEntry 
           { 
            vid = c.Field<int>("id"), 
            fname=c.Field<string>("fname"), 
            lname = c.Field<string>("lname"), 
            branch = c.Field<int>("branch"), 
            role = c.Field<int>("role") 

           }).FirstOrDefault(); 
       } 
       return callerentry; 
      } 
      catch (Exception) 
      { 

       throw; 
      } 
     } 

    } 

Getusereコードは、ログイン後、ユーザの他の詳細を取得することでプロジェクトに私のSQLデータベースに接続するための接続コードをwrittedています。ウェブAPIコードは、今、私は私がウェブAPIから資格情報を取得し、ログイン成功または失敗を進めて次のステップに取り組むことができません

[HttpGet] 
    [Route("api/Project/ValidateLogin/{email}/{password}")] 
    //e.g http://localhost:52352/api/Project/ValidateLogin/[email protected]/TEST123 
    public bool ValidateLogin(string email, string password) 
    { 
     return bal.ValidateLogin(email, password); 
    } 

が行われます。私は、ログイン後、次のページを入力する必要があります

角度コード:loginService.jsとして新しいJSファイル名を作成し、index.htmlをするか、メインページにloginService.jsファイルを含める

angular.module('myApp', []) 
    .controller('login', ['$scope', 
    function ($scope) { 
     $http.get('http://localhost:52352/api/Project/ValidateLogin/' + emailId) 
     $http.get('http://localhost:52352/api/Project/ValidateLogin/' + password) 
     function() 
     { 

     } 
     } 

      }); 
     }; 

    } 
    ]); 
+0

一般的に、私は以下を使用してログインAPIを起動し、ルーティングをAngularで管理している場合は応答を使用して処理を進めます。 var data = new FormData(); data.append( 'user'、userID); data.append( 'pass'、password); $ http.post( 'ログイン'、データ、{ \t withCredentials:偽、 \t transformRequest:angular.identity、 \tヘッダ:{ \t \t 'Content-Typeの':未定義 \t} \t})。成功(機能(レスポンス){ /// を操作するための応答を使用} – CrazyMac

答えて

0

(function() { 
    var loginService = function ($http) { 

    var baseURL = "http://localhost:52352";//Set one global variable to get base URL, So it will be easy to handle in future 

    var validateUser = function (email, password) { 
     var uri = baseURL + 'api/Project/ValidateLogin/' + email + "/" + password; 
     return $http.get(uri) 
     .then(function (result) { 
      return result.data; 
     }); 
    }; 

    return { 
     validateUser: validateUser 
    }; 
}; 

    angular.module('myApp').factory("loginService", ["$http", loginService]); 
}()); 
LoginController.jsとして新しいJSファイル名を作成し、index.htmlをするか、メインページにLoginController.jsファイルを含める

(function() { 
    var LoginController = function ($scope, loginService) { 
    $scope.login = function() { 
     //$scope.email and $scope.password these value you can get from ng-model from your Html control of views i.e. as follows 
     //<input type="text" name="email" class="form-control" ng-model="email" placeholder="email" /> 
     //<input type="password" name="password" class="form-control" ng-model="password" placeholder="password" />\ 

     loginService.validateUser($scope.email,$scope.password).then(function (response) { 
      if (response) { 
       $location.path('/home');//Redirect to homepage 
      } else { 
       return; 
      } 
     }, errorDetails); 
    } 

}; 
angular.module('myApp').controller("LoginController", ['$scope', 'loginService', LoginController]); 

}()); 
+0

なぜ2つのjsファイルが必要ですか?両方をloginController.jsファイルにマージできませんか? – beginner

+0

はい、コントローラにサービスコードを書くことはできますが、上記のコードを別々に使用すると、アプリケーションが大きくなったときに簡単に管理することができます。 これを読んでください: - https://docs.angularjs.org/guide/services https://docs.angularjs.org/guide/controller –

+0

J-平均:はいあなたは正しいですが、私はこれはアプリケーション全体のログイン部分であるため、単一のコントローラです。私はこのようなコードをマージしました。それが正しいか? – beginner

関連する問題