0

my factory is:

 myAppServices.factory('ProfileData',['$http',  function($http){
            return{
            newly_joined:function(callback){
            $http.get(
//myUrl will be an url from controller.
                myUrl
            ).success(callback);
              }
    };

          }
                                            ]);

and I have three controller which has different URL:

controller1:

AppControllers.controller('ProfileListCtrl',['$scope','$state', '$rootScope', 'ProfileData', '$timeout',   function($scope, $state, $rootScope, ProfileData, $timeout ) {


    ProfileData.newly_joined(function(response) {
      var myUrl= "www.abc...." 
     //something goes there
});

     }]);

controller2:

AppControllers.controller('ProfileListCtrl1',['$scope','$state', '$rootScope', 'ProfileData', '$timeout',   function($scope, $state, $rootScope, ProfileData, $timeout ) {


    ProfileData.newly_joined(function(response) {
      var myUrl= "www.abc...." 
     //something goes there
});

     }]);

and controller 3 is:

AppControllers.controller('ProfileListCtrl2',['$scope','$state', '$rootScope', 'ProfileData', '$timeout',   function($scope, $state, $rootScope, ProfileData, $timeout ) {


    ProfileData.newly_joined(function(response) {
      var myUrl= "www.abc...." 
     //something goes there
});

     }]);

I want different data in different controller because of different URL and I am showing all three details on single web page.

So if there were any method to send 'myUrl' in factory that I can use that for pulling data.

Note: please don't suggest me for using $resource or $routeparams because $resource was not successfull in pulling data from json and I don't want to use big variable Url for my page.

Thanks in advance

Ajay Kumar
  • 309
  • 4
  • 19
  • So depending on the controller you want different data returned? If that's the case, why not create 3 separate methods (I think that's the best design), or just pass a name to `newly_joined` like `newly_joined("profile")` vs. `newly_joined("profile1")` (not as good of a solution)? – dmeglio Mar 21 '16 at 13:16
  • I should also mention that the `success` callback on `$http` is deprecated, see https://docs.angularjs.org/api/ng/service/$http. You should be using `then` instead. – dmeglio Mar 21 '16 at 13:18

2 Answers2

0

All you need to do is add an additional parameter to the newly_joined function:

newly_joined:function(callback, myUrl){

Also, you should be using .then instead of .success

Austin
  • 1,291
  • 10
  • 19
0

Your factory should be returning promises instead of using callbacks.

myAppServices.factory('ProfileData',['$http',  function($http){
    return function(myUrl) {
        return $http.get(myUrl);
    };
}]);

The controller

AppControllers.controller('ProfileListCtrl',['$scope', 'ProfileData', function($scope,ProfileData) {

    var myUrl= "www.abc....";
    var httpPromise = ProfileData(myUrl);

    httpPromise.then(function onFulfilled(response) {
        $scope.data = response.data;
    }).catch(function onRejected(response) {
        console.log("ERROR ", response.status);
    });

}]);

The DEMO on JSFiddle

The advantage of using promises is that they retain error information.

Also notice that myUrl is sent to the factory as an argument.

For more information on the advantages of using promises, see Why are Callbacks from Promise Then Methods an Anti-Pattern?

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • hi @georgeawg, can u create a plunker here because I am getting error as following: http://errors.angularjs.org/1.4.4/$injector/undef?p0=ProfileData – Ajay Kumar Mar 22 '16 at 05:23
  • u can use this json file as a sample: http://www.w3schools.com/website/customers_mysql.php – Ajay Kumar Mar 22 '16 at 05:43