2016-09-02 3 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

함수 로그인이 필요 두 개의 매개 변수

그래서 매개 변수가없는 함수 로그인 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(); 

})에 호출 시도

;

희망이 도움이됩니다. 건배

관련 문제