Angular JS Service / Factory数据不适用于当时的功能

时间:2015-02-18 07:26:03

标签: angularjs service q angular-promise

我正在编写一个登录表单,该表单调用userAuth服务并检查凭据。

当我使用时,然后在我的usercontroller中我没有得到响应或者甚至没有执行函数。

userController.js

'use strict'; 

myApp.controller('userController',['sessionFactory','$scope', '$http','$rootScope','$state', '$stateParams','userAuth', function 
                                                            (sessionFactory,$scope,$http,$rootScope,$state, $stateParams,userAuth) {
$scope.user={email:'learner@gmail.com',password:'123456'}

$scope.sigin = function(credentials){

    console.log(userAuth.login(credentials)); //For Testing purpose only
    userAuth.login(credentials).then(function(response){
        console.log("I am inside then function");           //Not Executed
        console.log(response);                              // Not Executed
        });
console.log("I am out of then function");                   // Executed
$scope.loading = false;                                     // Executed

};


}]);

userAuth.js     myApp.service('userAuth',function($ http,$ rootScope,$ q){

var deferred = $q.defer();

this.login = function(credentials){
    credentials['action']='login';
    $http.post($rootScope.baseURL+'assets/api/user.php',credentials)
    .success(function(data,status,headers,config){

        deferred.resolve = "success";
        console.log(data);            //For test purpose only. Its returning data

    })
    .error(function(data,status,headers,config){

        deferred.reject = "failed";


    });

    return deferred.promise;


}



});

我错过了什么?Console.log output

1 个答案:

答案 0 :(得分:0)

deferred.resolve和deferred.reject是函数而不是变量,所以请尝试:

deferred.resolve("success");
deferred.reject("failed");