无法从angularjs工厂读取属性

时间:2015-02-17 06:53:11

标签: angularjs service factory

我正在尝试向sessionFactory发送请求以设置会话详细信息并设置成功操作。但我得到的错误是“无法读取属性”,然后是“未定义”。以下是我的工厂和控制器代码。

控制器代码

'use strict'; 

myApp.controller('userController',['sessionFactory','$scope', '$http','$rootScope','$state', '$stateParams', function (sessionFactory,$scope,$http,$rootScope,$state, $stateParams) {
$scope.login = function(){
    var credentials = {email:$scope.user.email,password:$scope.user.password,action:'login'};
    $http.post($rootScope.baseURL+'assets/api/user.php',credentials)
    .success(function(data,status,headers,config){
            if(data == 'false'){
                $scope.signinError = "username and password does not match";
            }
            else {
                $('#signInModal').modal('hide');
                sessionFactory.set(data).then(function(response){
                console.log(response);

                });
        }
    });
}
}]);

工厂代码

myApp.factory('sessionFactory',function($http,$rootScope){

   var session = {};

session.set = function(data){

    var credentials = data;
        credentials['action']='set';
          $http.post($rootScope.baseURL+'assets/api/session.php',credentials) //Sets session variables in server side.
          .success(function(response){
              if(response){
                    angular.forEach(response,function(value, key){
                    sessionStorage.setItem(key, value); //sets session variables in client side.

            });  
              }
              return response;
          });


}

return session;

});

1 个答案:

答案 0 :(得分:0)

关闭问题@ squiroid的回答:

  

返回$http.post($rootScope.baseURL+'assets/api/session.php',credentials)并处理控制器内的其他内容