1

I have written one factory which holds the data and I am using two controllers. I have passed one request to the factory to get the data. And then I am trying to display that data through another controller on a different page. I am successfully retrieving the data in one controller. But the moment I am trying to display that data in another html file though another controller nothing is coming up. I am not sure what went wrong.
This is factory function to fetch the data

productDetails.getProductDetails = function(sessionId,product){
        console.log("Getting the product details")
        var promise = $http({method : "POST",
            url : "some_url",
            data : {"some_data"}
        })
        promise.then(function(response){
            productDetails.details = response
            console.log(productDetails.details)
        }, function(err){
            console.log("Couldn't get the product details "+err)
        })
    }

This is how I am calling the factory function to get the data thhrough one controller

$scope.displayProductDetails = function(product){
        var promiseProductDetails = productService.getProductDetails($scope.userSession,product);
        window.location = "product-details.html"
    }

And then in second controller I am trying to console the same data. But nothing came up.

Any help would be highly appreciated

Regards

Lokesh Pandey
  • 1,739
  • 23
  • 50

1 Answers1

0

You need to return the value from factory

productDetails.getProductDetails = function(sessionId,product){
        console.log("Getting the product details")
        var promise = $http({method : "POST",
            url : "some_url",
            data : {"some_data"}
        })
        return promise.then(function(response){ // returning the promise
            productDetails.details = response
            return response // returning the resolved value
        }, function(err){
            return err
        })
    }
brk
  • 48,835
  • 10
  • 56
  • 78
  • I am returning the object which holds that value – Lokesh Pandey Jan 26 '18 at 09:23
  • @Lokesh work with promises, not variables – Aleksey Solovey Jan 26 '18 at 09:25
  • @AlekseySolovey yea i have made the request using promise itself. and if the response is there i am simply assigning that response to the object property and trying to display the property in another file – Lokesh Pandey Jan 26 '18 at 09:28
  • @Lokesh it's all asynchronous, you can't assign it to other variables when the data hasn't arrived yet. read more about it [**here**](https://stackoverflow.com/questions/12505760/processing-http-response-in-service) – Aleksey Solovey Jan 26 '18 at 09:33
  • @AlekseySolovey I figure that one out but somehow i am not getting the result. would you mind taking a look at the code – Lokesh Pandey Jan 26 '18 at 11:38
  • @Lokesh the more you explain, the more we can help you. What do you mean by "not getting the results"? Are you binding anything in your HTML? Are you using your values elsewhere? – Aleksey Solovey Jan 26 '18 at 11:50
  • @AlekseySolovey Yes like I have called factory function from one controller and I am trying to display that data through another controller in different page. And I am getting the value – Lokesh Pandey Jan 26 '18 at 11:56
  • @AlekseySolovey I am getting the data but when I am navigating to another page to display the same data. It's not coming up. And the point is I am storing that data in a factory variable and using that factory variable to display the data in another page. I tried console the data is there but I am not sure how I am going to print it in another page – Lokesh Pandey Jan 26 '18 at 12:08
  • @Lokesh **once again**, you can't store asynchronous data. Consider storing a promise (with data) instead. Possibly [this example](https://appendto.com/2016/02/working-promises-angularjs-services/) will help you to understand it better – Aleksey Solovey Jan 26 '18 at 12:15
  • @AlekseySolovey I tried the link which you have shared but again unable to display the data in another page – Lokesh Pandey Jan 26 '18 at 13:54
  • @Lokesh at this point you need to re-open a question with a full (relevant) section of your code that you have tried (in JSFiddle, Plunker, etc.) and describe why **many** suggestions given to you don't work. If you follow tutorials on "how to store asynchronous data with AngularJS", your code should work, but if it doesn't, then you have something else that affects your desired result. _This question and discussion should be closed_ – Aleksey Solovey Jan 26 '18 at 13:58