2016-09-02 10 views
0

コントローラからファクトリ関数を呼び出そうとしています。Angularjs Factoryname.functionが関数ではありません

マイコード:

angular.module("mainApp", ['ui.router', 'ngAnimate', 'toaster']) 

.factory("authenticationSvc", function($http, $q, $window) { 
    var userInfo; 

    function login(userName, password) { 
     var deferred = $q.defer(); 

     $http.post("/api/login", { 
      userName: userName, 
      password: password 
     }).then(function(result) { 
      userInfo = { 
       accessToken: result.data.access_token, 
       userName: result.data.userName 
      }; 
      $window.sessionStorage["userInfo"] = JSON.stringify(userInfo); 
      deferred.resolve(userInfo); 
     }, function(error) { 
      deferred.reject(error); 
     }); 

     return deferred.promise; 
    } 

    return { 
     login: login 
    }; 
}) 
.controller("LoginController", function($scope, toaster, $rootScope, $stateParams, $location, $http, authenticationSvc) { 
    $scope.login = authenticationSvc.login(); 
}); 

しかし、私はここで、以下のエラー

TypeError: authenticationSvc.login is not a function

+0

$ scope.login = ...にブレークポイントを設定しましたか? das authenticationSvcにはどのような値がありますか? –

答えて

0

を取得していますがどのように工場が動作していることを基本的な例です。以下のコードを変更してください

angular.module("mainApp", ['ui.router', 'ngAnimate', 'toaster']) 

.factory("authenticationSvc", function($http, $q, $window) { 
    var userInfo, authentication = {}; 

    authentication.login = function(userName, password) { 

     var deferred = $q.defer(); 

     $http.post("/api/login", { 
      userName: userName, 
      password: password 
     }).then(function(result) { 
      userInfo = { 
       accessToken: result.data.access_token, 
       userName: result.data.userName 
      }; 
      $window.sessionStorage["userInfo"] = JSON.stringify(userInfo); 
      deferred.resolve(userInfo); 
     }, function(error) { 
      deferred.reject(error); 
     }); 

     return deferred.promise; 
    } 

    return authentication; 
}) 
.controller("LoginController", function($scope, toaster, $rootScope, $stateParams, $location, $http, authenticationSvc) { 
    $scope.login = authenticationSvc.login(user,password); 
}); 

私はそれをテストしませんでしたが、確認してください。インサイドあなた工場に:

You just create an object, add properties to it, then return that same object. When you pass this service into your controller, those properties on the object will now be available in that controller through your factory.

0

機能ログインが必要2パラメータ

ので、パラメータのない関数でのログインがdefindedされていない

0

まず、工場のアプローチと定式化が行われていません正しく。

)は

.factory("authenticationSvc", function($http, $q, $window) { 
var userInfo; 

var authenticationSvc={}; 

//Define login() 
authenticationSvc.login=function(userName, password) { 
    var deferred = $q.defer(); 

    $http.post("/api/login", { 
     userName: userName, 
     password: password 
    }).then(function(result) { 
     userInfo = { 
      accessToken: result.data.access_token, 
      userName: result.data.userName 
     }; 
     $window.sessionStorage["userInfo"] = JSON.stringify(userInfo); 
     deferred.resolve(userInfo); 
    }, function(error) { 
     deferred.reject(error); 
    }); 

    return deferred.promise; 
} 

//return Factory Object 
return authenticationSvc; 

}) 

..以下のアプローチを使用しようとするとコントローラに、同じアプローチに

.controller("LoginController", function($scope, toaster, $rootScope, $stateParams, $location, $http, authenticationSvc) { 
$scope.login = authenticationSvc.login(); 

}を呼び出してみてください。

これが役に立ちます。 乾杯

関連する問題