in main module want create service parses json file , pushes contents array, after service have array returned accessible of controllers. issue function running before $http request complete returns empty array
dashmodule.factory('datafetch', function($http) { var emailarray = []; $http.get('../data/emails.json').success(function log(obj) { (var = 0; < obj.length; i++) { emailarray[i] = obj[i]; } }); return { test: function() { return emailarray; } }; });
use promise, like:
dashmodule.factory('datafetchservice', function($http) { var myreq = function() { return $http.get('../data/emails.json').then(function(response){ return response.data; }); }; return { myreq: myreq }; }); and
function getmyreq($scope, datafetchservice) { var myreqpromise = datafetchservice. myreq(); myreqpromise.then(function(result) { console.log(result); }); }
Comments
Post a Comment