Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active March 11, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onigetoc/514d163c0599be9c34d3 to your computer and use it in GitHub Desktop.
Save onigetoc/514d163c0599be9c34d3 to your computer and use it in GitHub Desktop.
Angular Factory load external json data - DataLoader
.factory('DataLoader', function ($http) {
return {
all: function (url) {
return $http.jsonp(url);
},
get: function (url) {
// Simple index lookup
console.log($http.get(url))
return $http.get(url);
},
post: function (url) {
// Simple index lookup
console.log($http.post(url))
return $http.post(url);
}
}
})
// EX: how to use it in your Controller
DataLoader.get(jsonURL).success(function (data, status, headers, config) {
$scope.post = data;
// Don't strip post html
$scope.content = $sce.trustAsHtml(data.content);
})
.error(function(data, status, headers, config) {
console.log('error');
});
})
// OR use it like this.
DataLoader.all( URLApi ).success(function(data, status, headers, config) {
$scope.posts = data;
//console.dir( data );
})
.error(function(data, status, headers, config) {
console.log('error');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment