0

I am getting data from the server with a service which is being called in this function:

LeadsSrv.async().then(function (d) {
    results = d.data;
});

I want to access the results outside of this function and then assign it to the scope, like this:

$scope.All_Leads = results;

Here is the server function:

LeadApp.service('LeadsSrv', function ($http) {
  var all = {
    async: function() {
        var promise = $http({
            url: '../app/Data.php',
            method: "POST",
            params: { req_id: 'leads_list', user_id: 1 }
        }).then(function (response) {
        return response;
      });
      return promise;
    }
  };
  return all;
});

How can i achieve this?

2
  • Is the server function being called in an Angular service or is it within a controller? Commented Mar 15, 2016 at 12:21
  • i just updated my question with the server function Commented Mar 15, 2016 at 12:32

4 Answers 4

3

You declare the variable at the beginning of your script, then whenever the function fetching the data returns, it will update the value of that variable.

$scope.results = [];

LeadsSrv.async().then(function (d) {
    $scope.results = d.data;
});

Thanks to how angular works, you won't need to do anything else, it will also update the UI elements and bindings as well.

Update:

I'd do it this way:

Service:

LeadApp.service('LeadsSrv', function ($http) {
    var service = {
        postData: postData
    };

    return service;

    function postData(data) {
        return $http.post('../app/Data.php', data);      
    }

In the Controller:

//Initialize the variable
$scope.results = [];

var data = {
    req_id: 'leads_list',
    user_id: 1
};

LeadsSrv.postData(data).then(function (response) {
    //Once the $http.post succeeds, it will update your variable
    $scope.results = response.data;
});

This should do exactly what you're looking for, unless I've misinterpreted your question.

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

4 Comments

yes your solutions works but am getting the data from the server which worked before also, but my question is how can i now access the scope var outside of the function to make it available on my entire site?
@jhondano could you please specify "entire site"? Is it all in angular, or is it a mix, etc?
The site is all in angular!
If you only use one controller, it should be available by $scope.results inside angular, or {{results}} in HTML.
0

I recommend going about this in following fasion.

Make your controller/directive ( whoever owns the $scope ) dependent on LeadSrv object. If it is not an "angular" component already wrap it in a factory, then inject it in your controller.

Then you simply do

LeadsSrv.async().then(function (d) {
    $scope.All_Leads = d.data;
});

Comments

0

Currently, you are doing the right thing, except of one mistake: Your call is getting executed asynchronously. So that basically means, results won't have the expected value the time you are referring it to $scope.All_Leads, since the declaration is happening synchronously.

To achieve what you want, you have to use promises. Take a look at this and Angulars documentation.

Comments

0

To check if your server is reporting an error, add a .catch method.

LeadsSrv.async().then(function onFulfilled(r) {
    console.log("DATA: ", r.data);
    $scope.results = r.data;
}).catch( function onRejected(response) {
    console.log("ERROR: ", response.status);
});

Comments

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.