0

I have a javascript function that retrieves a JSON object and the returns that value after processing it into a dataTable. Where I am running into trouble is returning that value to an Angular Scope. How would I retrieve the returned value inside the Angular Scope function?

Javascript

    function sessionsLength(){
    loadReport('session-length', 'days', oneMonth).then(function (results) {
        var i
        var rows = [];
        for (i = 0; i < results.length; i++) {
            var c = [];
            c.push(
                {v: results[i].timestamp.valueOf()},
                {v: results[i].data.tenth},
                {v: results[i].data.fiftieth},
                {v: results[i].data.ninetieth}
            );
            var segment = {c:c};
            rows.push(segment);
        }

        var dayPlot = {
            id: 'day',
            label: 'Day',
            type: 'number'
        };

        var tenthPlot = {
            id: 'tenth',
            label: 'Tenth',
            type: 'number'
        };

        var fiftiethPlot = {
            id: 'fiftieth',
            label: 'Fiftieth',
            type: 'number'
        };

        var ninetiethPlot = {
            id: 'ninetieth',
            label: 'Ninetieth',
            type: 'number'
        };

        var cols = [dayPlot,tenthPlot,fiftiethPlot,ninetiethPlot];

        var plot = {
            type:'Line',
            data:{
                'cols':
                    cols
                ,
                'rows':
                    rows
            },
            options: {
                chart:{
                    title: '',
                    subtitle: ''
                },
                height: 500
            }
        };
        return plot;
        console.log(plot);
    });
}

Angular Controller

'use strict';

angular.module('triAngularElements').
    controller('GoogleChartsLineController', function ($scope) {
        $scope.chartData = sessionsLength();
    });
Joseph Casey
  • 1,283
  • 1
  • 14
  • 34
  • You would need to return the promise and chain through to assign the value. There are many duplicates on these kind of questions. – PSL Jul 31 '15 at 00:20
  • Thanks @PSL, Mind linking an example? – Joseph Casey Jul 31 '15 at 00:22
  • 1
    Sure. Linked the best answer i could find. Great answers and great explanations. – PSL Jul 31 '15 at 00:24
  • Thanks, you did link to a great question and answer. Even more breadth then my question. Wish it had covered a little on promises with AngularJS, but beggars can't be choosers. – Joseph Casey Jul 31 '15 at 00:26
  • 1
    Well the concept is same. If you want to look at angular promises you have got many answer and documentation of $q promise as well.. Here ar e some.. http://stackoverflow.com/questions/12505760/processing-http-response-in-service , http://stackoverflow.com/questions/24595917/angularjs-http-return-value – PSL Jul 31 '15 at 00:27

0 Answers0