Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
up vote 8 down vote accepted

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

share|improve this answer
    
Sorry, i still not get the answer that i expected. I know if service must return function, so i describe the return in the last of my service. But it's not that i expected. See my complete explanation : laravel.io/bin/6LVej – nabilftd Apr 21 '15 at 7:34
    
I agree you know that the service must return the function. but its my suggestion to use this while working with service instead of var. BTW I have update the plunker. Please check and let me know if i am wrong – Bharat Bhushan Apr 21 '15 at 8:03
    
aaah, very thanks for you help..but now i still get the problem, i think the problem because i retrieve array from ng-model <div ng-repeat="i in getTimes()"><input type="number" ng-model="xi[$index]"></div>. if i write the array directly on addXi(arr[]) (ex addXi([1,2,3,4]), i get the result, but if i retrieve an array from ng-model, i get nothing. The console says "no data specified" :( – nabilftd Apr 22 '15 at 4:01
    
have any idea? @Bharat Bhushan – nabilftd Apr 24 '15 at 7:43
    
will you please produce the same case on any fiddle or plunker ! – Bharat Bhushan Apr 24 '15 at 7:56

Try following code

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

        $scope.lineChartData =shareDataService.getPlot();
});
share|improve this answer
    
Yeah, i've tried like that, but i get the same, nothing.Have any idea? – nabilftd Apr 21 '15 at 6:19
    
where are you calling addXi() and addYi() function? – samyak bhalerao Apr 21 '15 at 6:26
    
in RegresiController .controller('RegresiController', function($scope, shareDataService) { $scope.xi = []; $scope.yi = []; shareDataService.addXi($scope.xi); shareDataService.addYi($scope.yi); }); – nabilftd Apr 21 '15 at 6:43

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

$scope.lineChartData = $scope.getYo();
share|improve this answer
    
Yes, i know that, but first, i need the data to be like this [ [ [2,0], [3,1], [5,4] ] ]; I think if i do like that (if possible), i just get the data like this [ [2,0], [3,1], [5,4] ]; Have any idea? – nabilftd Apr 21 '15 at 6:27
    
Then it will be [$scope.getYo()]. – dfsq Apr 21 '15 at 6:32
    
haha, yapp, i tried like it too, and it's not working. because this code $scope.lineChartData = $scope.getYo();, if i call $scope.lineChartData from HTML, i get nothing. If i assign the $scope.getYo(); to $scope, i get nothing, but if i retun $scope.getYo(); from (example) $scope.getFoo(), i get the array. :( – nabilftd Apr 21 '15 at 6:40

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();
        };
share|improve this answer
    
Yeah, i've tried like that, but i get the same, nothing. But first, i need the data to be like this [ [ [2,0], [3,1], [5,4] ] ]; I think if i do like that (if possible), i just get the data like that [ [2,0], [3,1], [5,4] ]; Have any idea? – nabilftd Apr 21 '15 at 6:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.