简体   繁体   English

从控制器发送数据到工厂?

[英]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: 我有三个具有不同URL的控制器:

controller1: 控制器1:

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: 控制器2:

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: 控制器3为:

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. 由于URL不同,我想在不同的控制器中使用不同的数据,并且我在单个网页上显示所有三个详细信息。

So if there were any method to send 'myUrl' in factory that I can use that for pulling data. 因此,如果在工厂中有任何发送“ myUrl”的方法,我可以使用它来提取数据。

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. 注意:请不要建议我使用$ resource或$ routeparams,因为$ resource无法成功从json提取数据,并且我不想在页面上使用大变量Url。

Thanks in advance 提前致谢

All you need to do is add an additional parameter to the newly_joined function: 您需要做的就是向new_joined函数添加一个附加参数:

newly_joined:function(callback, myUrl){

Also, you should be using .then instead of .success 另外,您应该使用.then而不是.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 JSFiddle上DEMO

The advantage of using promises is that they retain error information. 使用promise的优点是它们保留错误信息。

Also notice that myUrl is sent to the factory as an argument. 还要注意, myUrl作为参数发送到工厂。

For more information on the advantages of using promises, see Why are Callbacks from Promise Then Methods an Anti-Pattern? 有关使用promise优点的更多信息,请参见为什么从Promise然后方法进行回调是一种反模式?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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