1

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

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

6 Comments

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
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
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" :(
have any idea? @Bharat Bhushan
will you please produce the same case on any fiddle or plunker !
|
2

Try following code

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

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

3 Comments

Yeah, i've tried like that, but i get the same, nothing.Have any idea?
where are you calling addXi() and addYi() function?
in RegresiController .controller('RegresiController', function($scope, shareDataService) { $scope.xi = []; $scope.yi = []; shareDataService.addXi($scope.xi); shareDataService.addYi($scope.yi); });
0

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

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

3 Comments

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?
Then it will be [$scope.getYo()].
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. :(
0

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();
        };

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.