2

i want to get just my service in the variable from my controller but it give me an $$state object

this is my controller

$scope.myDataSMS = ServiceSms.async().then(function(data){
    $scope.myDataSMS1 = data;
    console.log($scope.myDataSMS1);

    return $scope.myDataSMS1;
});

console.log($scope.myDataSMS);

and my service

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'}
            })

                .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;
});

i think that the problems is with the promise but there i m little bit stuck

if someone can help me please

thanks in advance

Gazano
  • 183
  • 2
  • 12
  • The question isn't clear enough on where is this '$$state object'. First of all, I'm sure it doesn't give you $$state object. It gives you an object (promise) that contains $$state property. Of course, `console.log($scope.myDataSMS)` will output a promise. Because myDataSMS is a promise. And myDataSMS1 is promise result. – Estus Flask Mar 27 '17 at 17:57

1 Answers1

1

this would be a better way to write your promise:

CONTROLLER:

.controller('nameofcontroller', ['$scope', 'ServiceSms', function($scope, ServiceSms) {
    $scope.myDataSMS = ServiceSms.async()
      .then(
        function(data){
          $scope.myDataSMS1 = data;
          console.log($scope.myDataSMS1);
          return $scope.myDataSMS1;
        },
        function(err){
          console.log('err: ' + err);
        });
}]);

SERVICE:

routeAppControllers.factory('ServiceSms', function($http,Token) {
   return {
      async: function() {
        var data = 'token=' + encodeURIComponent(key);
        return $http({
           method: 'POST',
           url: 'PhpFunction/getsms.php',
           data: data,
           headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        });
      }
    }
});
Paolo
  • 704
  • 9
  • 24
  • thanks for your reponse but it give me a promise, how a get my elements of this one Promise { : "pending" } – Gazano Mar 27 '17 at 17:29
  • You still need to act on the promise in the controller using `.then()`. Perhaps you're not setting your scope variable to the right object. Maybe try `$scope.myDataSMS1 = data.data;` – Ben Mar 27 '17 at 17:35
  • @Gazano be more clear please, also provide a fiddle or the error you are getting back if you can. – Paolo Mar 27 '17 at 17:37
  • @estus you are right, in this case returning a promise is redundant since $http already returns one. check the edit, it should be good now. Nice catch! – Paolo Mar 27 '17 at 18:22
  • Yes, it looks good now. I'm not sure if I understood the question correctly but probably $scope.myDataSMS shouldn't be assigned to the promise at all. It looks like the OP expected that it would be assigned to the result, and this won't happen. – Estus Flask Mar 27 '17 at 18:52
  • thanks for your help but even with the new edit i have always a $$state object for my var $scope.myDataSMS, may be there is a way to get the informations via $$state object ? – Gazano Mar 27 '17 at 20:16
  • this is a plunker http://plnkr.co/edit/QLFM2by2s5m8YLmXgTb4?p=preview , i m really need to get mydatasms outside the service and without $$state – Gazano Mar 27 '17 at 22:33
  • @Gazano i found a couple of posts that should help: 1) http://stackoverflow.com/questions/31974181/how-to-get-data-from-state-object 2) http://stackoverflow.com/questions/28146491/angular-http-returns-state-object – Paolo Mar 27 '17 at 23:09
  • @Gazano check the updated plnkr: http://plnkr.co/edit/ftwAzpiIrCvf3MEqJsf3 i hope it helps. i passed the callback to the factory and back the result in it. – Paolo Mar 27 '17 at 23:34
  • @PaoloMangia Thanks for your help but even with the update plunker i have the same $$state object :( Maybe there is a way to take the value of this $$state object with something like that : $scope.mydata.$$state.value ??? Because I have all my objects inside – Gazano Mar 28 '17 at 01:05
  • @Gazano are you getting $$state obj inside of the controller or even immediately after the $http call? try to log everything :) – Paolo Mar 28 '17 at 01:22
  • after the service call with this console.log console.log($scope.myDataSMS); = $$state object and this console inside the service console.log($scope.myDataSMS1); = array objet i want to get with $scope.myDataSMS after call the service, an array of object like this Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 1460 more… ] no like this Object { $$state: Object } – Gazano Mar 28 '17 at 11:02
  • @Gazano ok, last hope: check the plunker again :) http://plnkr.co/edit/ftwAzpiIrCvf3MEqJsf3 . you will see a .then inside both your factory and your controller, give it a go. – Paolo Mar 28 '17 at 12:16
  • @Gazano check this for further explanations: http://stackoverflow.com/a/28146566/5381735 – Paolo Mar 28 '17 at 12:18