how to put in variable a promise http (service angularjs) -
i have big problems, want put service in variable in controller
but there probleme, when call service http inside give me $$state object (impossible take value of one)
this code service , controller
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) { // function here opportunity modify response // console.log(data.data); // return value gets picked in controller. return data.data; }) // return promise controller return promise; } }; return myservice;
});
inside controller
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 }
but
for service token have no problems in variable because don't have http inside
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; });
i m stuck realy need var e = servicesms.async(); in controller , don't know how
please 1 can me
thanks in advance
edit
i put service have probleme var e , $$state object code , me need this
var e = servicesms.async().then(function(data) { console.log(data.data); // return me 1 array [ object, object, object, object, object, object, object, object, object, object, 1460 de plus… ] return data.data; // want var e outside of service because need table }); console.log(e); /// object { $$state: object } no
that's shown because @ time servicesms
response assign variable, promise not resolved.its still resolving. service return http controller promise still resolving. that's why $$state
display in console.
one thing can remove promise service , add promise controller. way can catch response after promise captured
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); }])
edited
var e ; servicesms.async().then(function(data) { console.log(data.data); // return me 1 array [ object, object, object, object, object, object, object, object, object, object, 1460 de plus… ] e = data.data; // want var e outside of service because need table console.log(e) });
Comments
Post a Comment