How to call a factory from a factory in AngularJS -


i can't find clear answer apologies in advance if there is.

(this plunker)http://plnkr.co/edit/emeqt9klapc9i1airabz?p=catalogue shows current state of code. there controller returns http query:

(function() { 'use strict'; angular .module('marketsapp') .controller('marketlocationcontroller', marketlocationcontroller);  marketlocationcontroller.$inject = ['$scope', '$routeparams',    'marketlocationservice'];  function marketlocationcontroller($scope, $routeparams, marketlocationservice) {     $scope.center = {};     $scope.markers = {};      // console.log(geo.coords);      activate ();      function activate () {         getmarketlocations();     }      function getmarketlocations() {         var marketlist = marketlocationservice.getmarkets();         $scope.marketlist = marketlist;         // insert list template here     } }  })(); 

the query takes 3 parameters, location factory:

(function() { 'use strict'; angular .module('marketsapp') .factory('browserlocation', browserlocation);  browserlocation.$inject = ['$q'];  function browserlocation() {      return {         getlocation: function () {             var deferred = $q.defer();              if (navigator.geolocation) {                 navigator.geolocation.getcurrentposition(function (position) {                     deferred.resolve(position);                 });             }             return deferred.promise;         }     }; } })(); 

and 2 parameters factory, either query or accessed local storage eventually:

(function() { 'use strict'; angular .module('marketsapp') .factory('userdefaultsservice', userdefaultsservice);  userdefaultsservice.$inject = ['$q'];  function userdefaultsservice($q) {      return {       getdefaults: function () {           var deferred = $q.defer();           var userdefaults = {               dist: 25,               limit: 100           };         return deferred.promise;         }     }; }  })(); 

and query constructed in factory:

(function() { 'use strict'; angular .module('marketsapp') .factory('marketlocationservice', marketlocationservice);  marketlocationservice.$inject = ['$http', '$q', 'browserlocation', 'userdefaultsservice'];  function marketlocationservice($http, $q, browserlocation, userdefaultsservice) {  var apiurl = "/api/markets/bylocation";      return {         getmarkets: getmarkets     };      function getmarkets () {          // var self = this;         var apiparams = {             point: null,             dist: null,             limit: null         };          var thislocation = null;         var thisdefault = null;          thislocation = function (location) {             browserlocation.getlocation()         };          thisdefault = function (defaults)   {             userdefaultsservice.getdefaults();         };          // extract params here         // (this how should work)         // lat = thislocation.coords.latitude;         // lon = thislocation.coords.longitude;         // apiparams.point = lat + "," lon;         // apiparams.dist = thisdefault.dist;         // apiparams.limit = thisdefault.limit;          $http({             url: apiurl,             method: 'get',             params: {                 point: point,                 dist: dist,                 limit: limit             }         });     } }  })(); 

i can't browserlocation , userdefaultsservice return objects can pass them apiparams. know have use $q or .then make sure both resolve @ moment don't seem have correct method of querying factories, question how query factory factory?

you can use $q.all wait multiple promises resolve. in marketlocationservice wait both browserlocation.getlocation , userdefaultsservice.getdefaults. when they're resolved execute $http request:

$q.all([browserlocation.getlocation(), userdefaultsservice.getdefaults()]).then(function(values) { // here values[0] response getlocation // values[1] response getdefaults           $http({             url: apiurl,             method: 'get',             params: {                 point: point,                 dist: dist,                 limit: limit             }         });         }); 

you need to resolve userdefaultsservice.getdefaults assuming should async:

return {       getdefaults: function () {           var deferred = $q.defer();           var userdefaults = {               dist: 25,               limit: 100           };           $timeout(function() {             deferred.resolve(userdefaults);           })         return deferred.promise;         }     }; 

see this plunker


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -