Last active
March 11, 2016 20:59
-
-
Save onigetoc/514d163c0599be9c34d3 to your computer and use it in GitHub Desktop.
Angular Factory load external json data - DataLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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