简体   繁体   中英

Sending data to factory from controller?

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM