angularjs - Deferred object returns wrong result when loading data by $http -
angularjs - Deferred object returns wrong result when loading data by $http -
i have mill loading json
info files using $http
, $q
in angularjs.
app.factory('dataloader', ['$http', '$q', function($http, $q){ var deferred = $q.defer(), info = {}; homecoming function (url){ info = $http.get(url) .success(function(response){ deferred.resolve(response); }) .error(function(){ deferred.reject('could not load template'); }); homecoming deferred.promise; } }]);
problem: everywhere utilize mill load data, returns first usage result. example:
loading file 1:
dataloader('json-folder/file-one.json').then(function(data){ console.log('result1: ', data); });
loading file 2:
dataloader('json-folder/file-two.json').then(function(data){ console.log('result2: ', data); });
in console have contents of file-one.json both. how can prepare dataloader factory?
a promise can resolved once. in addition, angular services singletons, sharing same promise whole app.
try create promise within returned function.
angularjs deferred
Comments
Post a Comment