3

I have written one factory to share data between controllers. Here is the sample

.factory('sharedData', function () {
        var property = {
            title: '',
            color: ''
        };

        return {
            getTitle: function () {
                return property.title;
            },
            setTitle: function (title) {
                property.title = title;
            },
            getColor: function () {
                return property.color;
            },
            setColor: function (color) {
                property.color = color;
            }
        };

I am using controller to set data in the factory using.

sharedData.setTitle("title");

but when I am trying to call:

console.log(sharedData.getTitle());

I am receiving empty string. I have injected service in both the controller properly. any help is appreciated.

Edit: Sorry to miss out one important information. I am setting the data after a successful REST call code is as below.

var req =
            {
                method: 'GET',
                url: "/titleName"
                headers: {
                    'Content-type': 'application/json'
                }

            };
            $http(req)
                .success(function (response, status)
                {
                    console.log(status);
                    console.log(response[0].name); 
                    sharedData.setTitle(response[0].name);
                    sharedData.setColor(response[1].color);

                })
                .error(function (response, status)
                {
                });

Json response is as below :

[{"name":"Name"},{"color":"red"}]

console.log in success callback is giving correct response as Name. Thanks

4
  • 3
    Likely some sort of asynchronous timing issue, would need to see more of usage code. Create a demo that replicates problem Commented Apr 27, 2015 at 11:09
  • might not be a reason, but you forgot comma , after setTitle in your example Commented Apr 27, 2015 at 11:10
  • no comma is not the issue that was a mistake from copy paste, i will edit the question Commented Apr 27, 2015 at 11:12
  • wouldn't have the problem if you shared reference to the actual object property and take advantage of prototypical inheritance Commented Apr 27, 2015 at 12:25

1 Answer 1

0

//EDIT

Ah i see you use a an async method. So yeah you should return the promise and resolve on success it.

Because the controller does't know if its async or not it just expects a value.

So while the service is retrieving the data the controller already asks for the data.

and then you will be returned an empty string.

When you return a promise you can resolve it in your routing or in your controller.

//Plunkr is old because data changed on the question Hi i have created a really small plunkr.

.controller('har', function(sharedData) {
   sharedData.setTitle("test")
   console.log(sharedData.getTitle())
   sharedData.setTitle("test2")
   console.log(sharedData.getTitle())
});

http://plnkr.co/edit/CvKgKtGmjCDxtk4dcbtv?p=preview

Check the console

I used your factory and a simple controller and for me it looks like it's working.

Do you have any other dependencies on it or do you use it differently.

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

1 Comment

yup that did it found another solution for working with promise in http get.

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.