0

How can one pass values from services to controllers? I have been reading stackoverflow questions regarding this and none of the solutions seem to solve my problem. I am trying to access google spreadsheets using tabletop.js When I console.log from services I can see the values however when I try to access the spreadsheet values from controller I get the following error: chartService.getProperty is not a function

The code for getting URL of the spreadsheet works fine. With get method. Not sure what I am doing wrong here.

Controller

angular.module('myapp')
  .controller('piechartCtrl', function (chartService, $scope, config) {
    $scope.values = chartService.getProperty();

  });

Service.js

angular.module('myapp')
.service('chartService', function(){
  return {
     getUrl: function init(path) {
        Tabletop.init( { key: path,
                         callback: showInfo,
                         simpleSheet: true } )
     }
  }

function showInfo(data, tabletop) {
    return{
      getProperty: function(){
        return data
      },
      setProperty: function(value){
        data = value;
      }

    }
  }
});
Imo
  • 1,455
  • 4
  • 28
  • 53
  • 3
    You haven't defined a `getProperty` method on the object literal you are returning from `chartService`? You have a `callback` param specified on the `Tabletop.init` method which makes me think you couldn't return it anyway, it might return a promise or something. – Daniel Lizik Jan 07 '16 at 16:36
  • check this answer [here](http://stackoverflow.com/questions/34681319/how-to-setup-service-to-pass-google-sheet-ids-angularjs/34792979#34792979) – puemos Jan 15 '16 at 18:42

1 Answers1

0

This is your service, the only thing i see you returning is the getUrl. so the only thing you will be able to access from the controller is chartService.getUrl function.

service('chartService', function ()
{
    return 
    {
        getUrl: function init(path)
        {
            Tabletop.init({
                key: path,
                callback: showInfo,
                simpleSheet: true
            })
        }
    }

    function showInfo(data, tabletop)
    {
        return 
        {
            getProperty: function ()
            {
                return data
            },
            setProperty: function (value)
            {
                data = value;
            }    
        }
    }
});

To Get it working, while I don't think this is the ideal solution it should work...

service('chartService', function ()
{
    var returnObject = 
    {
        getUrl: function init(path)
        {
            Tabletop.init({
                key: path,
                callback: showInfo,
                simpleSheet: true
            })
        },
        resultValue: {}
    }

    function showInfo(data, tabletop)
    {
        return 
        {
            getProperty: function ()
            {
                return data
            },
            setProperty: function (value)
            {
                data = value;
                returnObject.resultValue = value;
            }    
        }
    }

    return returnObject
});

then replace chartService.getProperty() with chartService.resultValue although this is in no was synchronous.

Delta
  • 869
  • 6
  • 12