Skip to main content
added 56 characters in body
Source Link
New Dev
  • 49.8k
  • 12
  • 99
  • 139

There are a number of things wrong with how you approach this.

First: you don't need to use $q.defer for something that already returns a promise, like $http - this is considered a deferred anti-pattern.

Second: avoid passing $scope to your service - it makes it more difficult to test and upsets separation of concerns. $scope belongs to the controller.

And so, your service can be made much simpler. You don't even need two methods - since both return the same thing

.factory("myDataService", function($http){
    return {
       getMetaData: function(){
          return $http.get('url/to/data');
       }
    };
}


.controller("MainCtrl", function($scope, myDataService){
   myDataService.getMetaData()
      .then(function(data)){
         $scope.metaD = data;
      });
}

There are a number of things wrong with how you approach this.

First: you don't need to use $q.defer for something that already returns a promise, like $http - this is considered a deferred anti-pattern.

Second: avoid passing $scope to your service - it makes it more difficult to test and upsets separation of concerns. $scope belongs to the controller.

And so, your service can be made much simpler. You don't even need two methods - since both return the same thing

.factory("myDataService"
    getMetaData: function(){
       return $http.get('url/to/data');
    }
}


.controller("MainCtrl", function($scope, myDataService){
   myDataService.getMetaData()
      .then(function(data)){
         $scope.metaD = data;
      });
}

There are a number of things wrong with how you approach this.

First: you don't need to use $q.defer for something that already returns a promise, like $http - this is considered a deferred anti-pattern.

Second: avoid passing $scope to your service - it makes it more difficult to test and upsets separation of concerns. $scope belongs to the controller.

And so, your service can be made much simpler. You don't even need two methods - since both return the same thing

.factory("myDataService", function($http){
    return {
       getMetaData: function(){
          return $http.get('url/to/data');
       }
    };
}


.controller("MainCtrl", function($scope, myDataService){
   myDataService.getMetaData()
      .then(function(data)){
         $scope.metaD = data;
      });
}
Source Link
New Dev
  • 49.8k
  • 12
  • 99
  • 139

There are a number of things wrong with how you approach this.

First: you don't need to use $q.defer for something that already returns a promise, like $http - this is considered a deferred anti-pattern.

Second: avoid passing $scope to your service - it makes it more difficult to test and upsets separation of concerns. $scope belongs to the controller.

And so, your service can be made much simpler. You don't even need two methods - since both return the same thing

.factory("myDataService"
    getMetaData: function(){
       return $http.get('url/to/data');
    }
}


.controller("MainCtrl", function($scope, myDataService){
   myDataService.getMetaData()
      .then(function(data)){
         $scope.metaD = data;
      });
}