如何从角度提供服务数据

时间:2015-04-17 07:02:37

标签: javascript angularjs

我有LoginControllersecurityService

这是LoginCtrl

// place the message if something goes wrong
$scope.authMsg = '';
$scope.login = function () {
        $scope.authMsg = '';
        var loginData = {email: $scope.account.email, password: $scope.account.password};
        securityService.login(loginData);
    };

这是securityService

login: function (logData) {
    var _vm = this;

    $http
        .post('/api-token-auth/', logData)
        .then(function (response) {
            // assumes if ok, response is an object with some data, if not, a string with error
            // customize according to your api
            if (!response.data.token) {
                _vm.authMsg = 'Incorrect credentials.';
            } else {
                $cookieStore.put('djangotoken', response.data.token);
                $http.defaults.headers.common.Authorization = 'JWT ' + response.data.token;
                $http.get('/api/account/restricted/').then(function (response) {
                    authService.loginConfirmed();
                    _vm.currentUser = response.data;
                    $rootScope.currentUser = response.data;

                });
            }
        }, function (x) {
            _vm.authMsg = 'Server Request Error';
        });
},

此登录工作正常,但我的问题是我不知道如何将authMesg从服务转移到控制器,因为这是异步。每次我登录无效时都会收到空白消息

2 个答案:

答案 0 :(得分:2)

您需要使用角度的承诺服务来使您成为控制器和服务同步

 login: function (logData) {
                    var _vm = this,d= $$q.defer();

                    $http
                        .post('/api-token-auth/', logData)
                        .then(function (response) {
                            // assumes if ok, response is an object with some data, if not, a string with error
                            // customize according to your api
                            if (!response.data.token) {
                                _vm.authMsg = 'Incorrect credentials.';
                            } else {
                                $cookieStore.put('djangotoken', response.data.token);
                                $http.defaults.headers.common.Authorization = 'JWT ' + response.data.token;
                                $http.get('/api/account/restricted/').then(function (response) {
                                    authService.loginConfirmed();
                                    _vm.currentUser = response.data;
                                    $rootScope.currentUser = response.data;

                                });
                            }
                            d.resolve(vm.authMsg);

                        }, function (x) {
                            _vm.authMsg = 'Server Request Error';
                              d.reject(vm.authMsg);
                        });
                },

在控制器中,您需要解决此承诺

securityService.login(loginData).then(function(data){
 consol.log(data); // get success data

},function(error){
   consol.log(data); // get error message data 
})

并在您的服务中注入 $ q

答案 1 :(得分:0)

这将为您提供authMsg

securityService.login(loginData).authMsg

但请关注@Vigneswaran Marimuthu评论,这是最佳做法。