2016-09-28 41 views
0

我被困在一個問題上,我使用角度和控制器發送數據到我的工廠,我有方法必須發佈用戶名,登錄等到我的終點,我在哪裏生成進一步使用智威湯遜,但不知何故,我得到這個錯誤,不知道爲什麼它甚至發生。無法讀取未定義的角廠的屬性'帖子'

任何想法可能導致此問題?

類型錯誤:在Object.login(Auth.js:22)無法讀取的不確定 財產 '後'。 在Scope.LoginController $ scope.signIn(LoginController.js:11)

這裏是我的這裏工廠POST請求我的終點

'use strict'; 
angular.module("Diplomka").factory('oauth', [function oauth($http) { 
    'use strict'; 
    return { 
     login: function (username, password) { 
      var token = ""; 
      var config = { 
       headers: { 
        "Content-Type": "application/x-www-form-urlencoded" 
       } 
      } 

      var data = { 
       username: username, 
       password: password, 
       grant_type: "password" 
      } 

      $http.post("/Token", data, config) 
       .then(function (response) { 
        token = response.access_token; 
       }); 
     } 
    }; 
}]); 

是我的控制器

'use strict'; 
app.controller('LoginController', ['$scope', 'oauth', '$location', LoginController]); 
function LoginController($scope, oauth, $location) { 


    $scope.username = ""; 
    $scope.password = ""; 

    $scope.signIn = function() { 
     oauth.login($scope.username, $scope.password); 
    } 

}; 

回答

0

你有你的「數組語法」錯誤。您的服務名稱和作爲參數傳遞給工廠構造函數的變量之間存在不匹配。

'use strict'; 
angular.module("Diplomka").factory('oauth', ['$rootScope', '$http', function oauth($scope, $http) { // It should be like this 
    // Rest of your factory here 
}]); 

另外,我不認爲這是注入$rootScope貴廠使用變量名$scope一個好主意。相反,我會將其稱爲$rootScope並替換您工廠中的所有$scope參考。

'use strict'; 
angular.module("Diplomka").factory('oauth', ['$rootScope', '$http', function oauth($rootScope, $http) { // It should be like this 
    // Rest of your factory here, replacing $scope with $rootScope 
}]); 
+0

哦,我在那裏犯了一個愚蠢的錯誤,非常感謝,現在它的工作 – user3188501

相關問題