2016-09-28 8 views
0

私は1つの問題に悩まされています。私は、私の工場にデータを送信するための角度とコントローラを使用します。さらなる使用のためにJWTを生成しますが、どういうわけかこのエラーが発生し、なぜそれが起こるのかわかりません。定義されていない角度のファクトリのプロパティ 'post'を読み取ることができません

この問題の原因は何ですか?

はTypeError:Object.login(Auth.js:22)で、未定義 のプロパティ 'ポスト' を読み込めません。Scope.LoginControllerで $ scope.signIn(LoginController.js:11)ここ

は私ですここに私のエンドポイントへのポスト要求

'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 
}]); 

はまた、私はそれが変数名$scopeを使用して、工場出荷時に$rootScopeを注入するのは良いアイデアだとは思いません。代わりに私はそれを$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

関連する問題