Called service data


I using this to save data to the service and called it again in the same controller after save it. But it didn’t have value for the service. When i call the service again in another controller, it gave me the result i want.

.controller('HomeCtrl', function ($scope) {

       Service.getData(); // value = 1000

    // call save data from service
    // i didn't get any data from the service
    Service.getData(); // value = undefined

So how do i get the data from service except inside the http.get??


Hey there!

One way to do this would be to use promises in your service. Here’s how it would look with your example.

.factory('Service', [
        return getData: function() {
            var def = $q.defer();

            // Return the promise
            return def.promise

From here, you could use the factory in your controller like so:

    console.log('Data from a service!', data);

Let me know if that helps out :slight_smile:

Awesome, it works. Thanks :smile: