Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable to factory angularjs

Tags:

angularjs

I would like te pass a variable to a factory of mine, but im not quite sure how to do it, here is my code:

var app = angular.module('docFinder', []);

app.factory('docFactory', function($http) {
  var url = 'http://test.example.com?queryString=searchv2&page='; 
  url=url+page;
  var docFactory = {
    async: function() {
      var promise = $http.get(url).then(function (response) {
        return response.data;
      });
      return promise;
    }
  };
  return docFactory;
});

app.controller('docTable', function(docFactory, $scope, $filter) {

    docFactory.async().then(function(d) {   
        $scope.providers = d;       
        init();
    });

}

i would like to send the page from my controller to my factory so it can return my new query

thanks

like image 690
Rodrigo Zurek Avatar asked May 28 '13 20:05

Rodrigo Zurek


1 Answers

You can pass the value through your async function in your factory:

var docFactory = {
    async: function(theVarThatIWantedToPass) {
        var url=//   stuff
        url += theVarThatIWantedToPass;
    }
}

Called as normal: docFactory.async(page)

like image 161
rGil Avatar answered Oct 23 '22 19:10

rGil