0

My approach is using AngularJS Service instead of factory along the project. Now I want to build an object called MyData which should be used as array in angularJS controller, such as:

 vm.datas= MyData.query({}, function (datas) {
         vm.thisData = {selected: datas[0].id};
    }); 

As I searched in questions, I understood I can use factory as below:

angular.module('myDataService', ['ngResource']).
factory('MyData', function($resource){
return $resource(myURL, {}, {
    query: {method:'GET', isArray:true}
});
});

What should I do if I want to use service. Is the code correct, below? What is the best practice?

angular
        .module('app')
        .service('myDataService', myDataService);
myDataService.$inject = ['ngResource'];

function myDataService($resource) {
    var self = this;
    self.MyData = $resource(myURL, {}, {
        query: { method: 'GET', isArray: true }
    });

}
Elnaz
  • 2,854
  • 3
  • 29
  • 41
  • Do you really need a different `app.module('myDataService')` for the factory? you could create one in the same module- `app.module('app')` – Sajal May 21 '16 at 09:45
  • Sorry,I didn't understand, would you please explain it more? I want to use just service and not factory. but I don't know if I can use $resource in service as above code, exactly as its usage in factory or not. – Elnaz May 21 '16 at 09:49
  • 1
    See if this helps: http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory/13763886#13763886 – Sajal May 21 '16 at 10:19

0 Answers0