2017-07-16 86 views
0

我試圖實現用戶身份驗證,但不知何故,我遇到了麻煩,因爲它不工作。我沒有得到任何具體的錯誤,但當我按下登錄按鈕時,它什麼都不做。 當我在Chrome開發工具上打開「網絡」選項卡時,看不到任何API調用正在進行。不能連接工廠到控制器

這是事先我authServices.js

angular.module('authServices', []) 
.factory('Auth', ['$http', function ($http) { 
    var authFactory = {}; 

    authFactory.doLogin = function (loginData) { 
     return $http({ 
      method: 'POST', 
      url: 'http://localhost:3000/api/users/authenticate', 
      data: loginData 
     }).then(function (data) { 
      console.log(data.data.token); 
      return data; 
     }) 
    }; 
    return authFactory; 
}]); 

login.js

angular.module('logInUserCtrl', ['authServices']) 
.controller('logCtrl', ['$scope', '$location', '$http', '$timeout', 
'Auth', function ($scope, $location, $http, $timeout, Auth) { 

    $scope.doLogin = function (loginData) { 
     $scope.loading = true; 
     $scope.errorMsg = false; 

     Auth.login(app.loginData) 
      .then(function (data) { 
       if (data.data.success) { 
        $scope.loading = false; 
        $scope.successMsg = data.data.message + 'Redirecting...'; 
        $timeout(function() { 
         $location.path('/') 
        }, 2000); 
       } else { 
        $scope.loading = false; 
        $scope.errorMsg = data.data.message; 
       } 
      }) 
    } 
} 
]) 

和我login.html

<form ng-submit="logCtrl.doLogin(loginData)"> 
      <label>E-Mail:</label> 
      <input class="form-control" type="text" name="email" placeholder="please enter email" 
        ng-model="loginData.email"> 
      <br> 
      <label>Username:</label> 
      <input class="form-control" type="text" name="username" placeholder="please enter username" 
        ng-model="loginData.username"> 
      <br> 
      <label>Password:</label> 
      <input class="form-control" type="password" name="password" placeholder="please enter password" 
        ng-model="loginData.password"> 
      <br> 
      <button class="btn btn-primary" type="submit" formmethod="post">Login</button> 
     </form> 

謝謝!

回答

2

既然你不使用Controller as syntax,更改按鈕功能,

<form ng-submit="doLogin(loginData)"> 

也確保您在HTML樣子控制器,

<div ng-controller="logCtrl"> 
+1

它的工作!非常感謝 :) – XhensB