I tried creating a service that will get data from a local JSON file, but it's not working, and there's no explanation why not.
The plunkr is here. Here's my code for the service:
webTestApp.factory('webtest', function($q, $timeout, $http) {
var Webtest = {
fetch: function(callback) {
var ret = function() {
$http.get('webtest.json').success(function(data) {
return data;
});
};
return ret();
}
};
return Webtest;
});
The Plunkr above is exactly what I was doing in my project, but I forked another Plunkr where someone got the same sort of thing was working. I found it at this StackOverflow answer.
Here's a working version
webTestApp.factory('webtest', function($q, $timeout, $http) {
var Webtest = {
fetch: function(callback) {
var deferred = $q.defer();
$timeout(function() {
$http.get('webtest.json').success(function(data) {
deferred.resolve(data);
});
}, 30);
return deferred.promise;
}
};
return Webtest;
});
My question is, why does my version (the first block) not work, but the second one does?