2016-06-16 106 views
0

我對angularjs非常陌生,我看過很多網頁,但沒有得到任何具體的解決方案。angular.js:12520 TypeError:無法讀取未定義的屬性'post'

我想實現的是我創建一個工廠服務來驗證用戶。成功驗證後,導航至儀表板頁面。

我收到以下錯誤:

angular.js:12520 TypeError: Cannot read property 'post' of undefined 
    at Object.factory.authentication (login.html:74) 
    at r.$scope.loginCall (login.html:65) 
    at fn (eval at compile (angular.js:13365), <anonymous>:4:218) 
    at e (angular.js:23613) 
    at r.$eval (angular.js:16052) 
    at r.$apply (angular.js:16152) 
    at HTMLInputElement.<anonymous> (angular.js:23618) 
    at HTMLInputElement.dispatch (jquery-1.9.1.min.js:3) 
    at HTMLInputElement.v.handle (jquery-1.9.1.min.js:3) 

我的HTML代碼:

<div ng-app="loginFormApp" ng-controller="loginFormCtrl"> 

<form method="post" action="" id="login_form" class="row"> 
          <input type="text" placeholder="Login ID" ng-model="loginId" > 
          <input type="password" placeholder="Password" ng-model="password" > 
          <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Login"> 
          <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Register Here"> 
         </form> 
         </div> 

我的控制器:

var app = angular.module('loginFormApp', []); 
      app.controller('loginFormCtrl', function($scope, $http, AuthService) { 
       $scope.loginCall = function() { 

        AuthService.authentication(); 

       }; 
      }); 

我廠的服務是:

app.factory('AuthService', function() { 
      var factory = {}; 
      factory.authentication = function($http) { 
       //alert("hello1"); 
       $http.post("http://103.19.89.152:8080/ccp-services/authenticate", { 
         'userName': $scope.loginId, 
         'password': $scope.password 
        }) 
        .then(function(response) { 

         window.location.href = "http://103.19.89.152:8080/earnfit/ui/angular/dashboard.html"; 
        }); 
      }; 
      return factory; 
     }); 

回答

1

在Factory方法中,您不注入$ http,因此在您訪問它時未定義它。

請參閱所以下面的代碼

app.factory('AuthService', function ($http) { 
return { 
    authentication : function (UserName, Password) { 
     $http.post("http://103.19.89.152:8080/ccp-services/authenticate", { 'userName': UserName, 'password': Password}) 
      .then(function (response) { window.location.href = "http://103.19.89.152:8080/earnfit/ui/angular/dashboard.html";}, 
        // Error Handling 
        function (response) {console.log(response)}); 
    } 
    } 
}); 

,如果你看到我刪除的範圍,而是我傳遞的登錄數據作爲參數,將作爲注入$範圍不推薦。

- 法爾漢

+0

上述評論是否幫助您解決該問題? –

+0

是的,這對我有用謝謝Syan Farhan非常感謝您的幫助。 –

+0

太棒了。歡迎你Bhai :) –

0

$http 作爲工廠依賴線。

app.factory('AuthService', function($http) { 

問候。

+0

HVA narola:我添加$ HTTP作爲對工廠生產線的依賴,但它給angular.js:12520Error:[$注射器:unpr]錯誤。 –

相關問題