如何输入变量一个承诺http(service angularjs)

时间:2017-03-28 12:24:31

标签: angularjs

我有很大的问题,我想把一个服务放在我的控制器中的变量

但是有一个问题,当一个带有http的服务调用它给我一个$$状态对象(不可能取这一个的值)

这是我的代码服务和控制器

routeAppControllers.factory('ServiceSms', function($http,Token) {

var key = Token.CreateToken()

var myService = {

    async: function() {

        var data = 'token=' + encodeURIComponent(key);
        var promise =  $http({
            method: 'POST',
            url: 'PhpFunction/getsms.php',
            data: data,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            cache: true
        })

            .then(function(data) {

                // The then function here is an opportunity to modify the response
                //  console.log(data.data);
                // The return value gets picked up by the then in the controller.
                return data.data;
            })

        // Return the promise to the controller
        return promise;
    }
};
return myService;

});

控制器内的

    routeAppControllers.controller('customersCtrl',['$scope','$rootScope','$filter','$log','$http','$cookies','$timeout','$uibModal','ServiceGetData','sha256','ServiceSms', function($scope,$rootScope, $filter, $log, $http,$cookies, $timeout, $uibModal,ServiceGetData,sha256,ServiceSms ) {

          var e =  ServiceSms.async();
        console.log(e);
}])

reponse:Object {$$ state:Object}

但是

我的服务令牌我没有问题,因为我没有内部的http

    routeAppControllers.factory('Token', function(sha256) {

   var myService =   {

       CreateToken: function () {

           var date = new Date();

           var time = date.getTime().toString();

           var string = time;

           var key = sha256.convertToSHA256(string);

           return key;

       }


   }

   return myService;

});

我卡住了我真的需要我的var e = ServiceSms.async();在我的控制器中,但我不知道如何

请某人帮助我

提前致谢

修改

我把服务和你一样,但我总是遇到问题,我的var,总是带有你的代码的$$状态对象,我需要这样的东西

var e = ServiceSms.async().then(function(data) {

       console.log(data.data); // return me this one Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 1460 de plus… ] good 
       return data.data; // i want get with var e outside of service because i need this table 
    });
    console.log(e); /// Object { $$state: Object } no good

1 个答案:

答案 0 :(得分:0)

显示的是因为当ServiceSms响应分配给变量时,承诺未解决。它仍在解析。 service将http返回给控制器,但承诺仍在解决。这就是$$state在控制台中显示的原因。

您可以做的一件事是从服务中删除承诺并将承诺添加到控制器。这样,您可以在捕获承诺后捕获响应

routeAppControllers.factory('ServiceSms', function($http, Token) {
    var key = Token.CreateToken()
    var myService = {
        async: function() {
            var data = 'token=' + encodeURIComponent(key);
            var promise = $http({
                method: 'POST',
                url: 'PhpFunction/getsms.php',
                data: data,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                cache: true
            })
            return promise;
        }
    };
    return myService;
})
routeAppControllers.controller('customersCtrl', ['$scope', '$rootScope', '$filter', '$log', '$http', '$cookies', '$timeout', '$uibModal', 'ServiceGetData', 'sha256', 'ServiceSms', function($scope, $rootScope, $filter, $log, $http, $cookies, $timeout, $uibModal, ServiceGetData, sha256, ServiceSms) {
    ServiceSms.async().then(function(data) {
        var e = data.data;
    });
    console.log(e);
}])

被修改

var e ;
 ServiceSms.async().then(function(data) {

       console.log(data.data); // return me this one Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 1460 de plus… ] good 
        e  = data.data; // i want get with var e outside of service because i need this table 
       console.log(e)
    });