0

I have this Angular JS Service:

'use strict';
app.factory('loggedService', ['$http', 'authService', 'customerService', function ($http, authService, customerService) {
    var out = [];

    if (authService.authentication.isAuth == false) {
        $location.path('/login');
        out = "effettuare login";
    }
    else {

        customerService.getCustomerAnagrafica().then(

            function (results) {

            out = results.data;

        }, function (error) {
            //alert(error.data.message);
        });
    }
    return { out: out };
}]);

What I would like to return the value of results.data.

I wanted to do something like the trick _this = this; and I tried to put that code after .then( with no success.

My goal is to geth the results.data out of that service.

to be called from a controller simply like this: $scope.myResults = loggedService.out;

4
  • You should return the promise from the service and populate the result out of the service. Commented Jul 31, 2015 at 17:56
  • 1
    What is returned; {out:undefined} ..? Commented Jul 31, 2015 at 17:58
  • If out is undefined, then it means that results.data is undefined, otherwise it would be an empty Array. Commented Jul 31, 2015 at 18:01
  • results.data is populated, the problem is that out in out = results.data; is not the same as var out = [].... Infact my output is [] Commented Jul 31, 2015 at 18:04

3 Answers 3

1

You could use an Observer but it is not the best way. Your factory should return the promise and your Controller should use it to interact in $scope.

Every time your service has to interact with scope, you must use the promises as well.

Sign up to request clarification or add additional context in comments.

Comments

1

You are creating a race condition by not working with the promise and using a closure instead. In other words, when you use your closure for other things your asynchronous job might have not populated the closure yet.

This is basically what you are doing:

var closure = 'not yet';

var getReady = function(){
  var func = function(){
      closure = 'i am ready.';
    }
  setTimeout(func, 1000);
};

getReady();
alert(closure);
//not ready yet (because it takes 1000 ms).

What you need to do is keep on working with the promise, in sequence.

    function constructHouse() {
        var house = new Promise(buildHouse);
        var paintedHouse = house.then(paintHouse);
        var cleanHouse = paintedHouse.then(cleanHouse);
        return cleanHouse;
    }
    //You can keep on working in sequence with the returned promise:
    constructHouse().then(sellHouse); 

1 Comment

I think as I adopted the above solution that I understood that i am falling in this mistake.
0

You need to return a promise .. So your code.. and sorry I am writing this in the browser.. so I may have a typo..

'use strict';
app.factory('loggedService', ['$q','$http', 'authService', 'customerService', function ($q,$http, authService, customerService) {

  var deferred = $q.defer();

  if (authService.authentication.isAuth == false) {
    $location.path('/login');
    deferred.resolve("effettuare login");
  }
  else {
    customerService.getCustomerAnagrafica().then(
        function (results) {
        deferred.resolve(results.data)
    }, function (error) {
        //alert(error.data.message);
    });
  }
  return deferred;
}]);

The you can call loggedService.promise.then(function(data){ ....

3 Comments

loggedService.then(function (data) { $scope.pp = data; }); It tells me that loggedService doesn't support .then method
No need to use another promise for this. customerService.getCustomerAnagrafica().then(); should return a promise (as a thenable object it should if it's following Promises A+). So you need to return that. Then loggedService.then should work. However, the resolve function should return results.data instead of deferred.resolve(results.data);
from the controller i run this: loggedService.then(function (data) { _this.pp = data; }); But when I run the console returns that then method is not a method of loggedService

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.