http post回调不工作角工厂

时间:2017-01-02 11:21:37

标签: angularjs http angular-services

我试图从角度服务获得回调,但回调函数不起作用。

这是我的控制器代码: -

$scope.uu = "hello"  // this i just make for test
$scope.login = function(user){
    //console.log(user)
    AuthenticationService.Login(user.username,user.password).success(function(data){
        console.log(data);
        $scope.uu = data;
    })
}

我的服务代码是: -

    mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){
    var service = {};
    service.Login = function (username, password, callback) {

            $http({
        method: "POST",
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: 'users/authenticate',
        data: {email: username,password:password}
    })
    .success(callback);
        };
        return service;
});

我都没有在控制台中得到回复。范围都没有改变。 我已经提到了这个问题,但答案对我不起作用。 $http POST response from service to controller

1 个答案:

答案 0 :(得分:1)

如果您正在编写服务,您的代码应如下所示:

mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){
    
    this.Login = function (username, password, callback) {

            $http({
        method: "POST",
        //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: 'users/authenticate',
        data: {email: username,password:password}
    })
    .success(callback);
        };
        
});

永远记住服务就像原型功能一样 我们不应该在服务中返回对象。 Angular实例化对象并自动返回

相关问题