nmfzone
nmfzone

Reputation: 2903

How to pass array from service function to scope in controller (AngularJs)?

I have an angular service shown below :

    .service('shareDataService', function() {
            var myXi = [];
            var myYi = [];

            var addXi = function(newObj) {
                myXi = newObj;
            }

            var addYi = function(newObj) {
                myYi = newObj;
            }

            var getPlot = function() {
                var a = [];

                for (var i = 0; i < myXi.length; i++) {
                    a.push([myXi[i], myYi[i]]);
                }

                return a;
            }

            return {
                addXi: addXi,
                addYi: addYi,
                getPlot: getPlot
            };
    });

And i have angular controller :

    .controller('plotController', function($scope, shareDataService) {
            $scope.getYo = function() {
                    return shareDataService.getPlot();
            };
            $scope.lineChartData = [
                    $scope.getYo()
            ];
    });

I need to send a value from $scope.getYo to $scope.lineChartData (it will be and array). How can I do that? I've tried like that, but if I call $scope.lineChartData in HTML, I get nothing. The data that I need is

    [
            [
                    [2,0],
                    [3,1],
                    [5,4]
            ]
    ];

UPDATE :

See my complete explanation in http://laravel.io/bin/6LVej . Thanks

Upvotes: 1

Views: 13684

Answers (4)

Bharat Bhushan
Bharat Bhushan

Reputation: 2097

As we know factory and services are basically used for sharing the data. But both have different use and meaning. The basic difference is Factory should always return Object and Service should return Function(constructor function in JS). Here is code snippet that may be helpful in your case.

    var app = angular.module('plunker', []);

app.service('arrayService', function() {
  return function() {
    this.getArray = function() {
      return [1, 2, 3, 4, 5];
    }
  }
})
app.factory('arrayFactory', function() {
  return {
    getArray : function() {
      return [9,8,7,6,5];
    }
  }
})
app.controller('MainCtrl', ['$scope','arrayService','arrayFactory',
  function($scope, arrayService,arrayFactory) {
    var ary = new arrayService();
    $scope.serviceArrayObject = ary.getArray();
     $scope.factoryArrayObject = arrayFactory.getArray()

  }
]);

Here is the plunker for basic difference How we use service and factory

Upvotes: 8

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38713

this shareDataService.getPlot(); is return the array object, So you can directly call this to $scope.lineChartData. try this

 .controller('plotController', function($scope, shareDataService) {             
            $scope.lineChartData = shareDataService.getPlot();
    });

You don't need write this code

        $scope.getYo = function() {
                return shareDataService.getPlot();
        };

Upvotes: 0

samyak bhalerao
samyak bhalerao

Reputation: 307

Try following code

.controller('plotController', function($scope, shareDataService) {

        $scope.lineChartData =shareDataService.getPlot();
});

Upvotes: 2

dfsq
dfsq

Reputation: 193301

Since getYo returns an array, you can simply assign its return value to lineChartData directly:

$scope.lineChartData = $scope.getYo();

Upvotes: 0

Related Questions